0

Is there a broad aversion in the C# community to modifying parameters? For example, would a popular style-checker complain about the parameter reassignment aspect of the following?

public void Create(Template template = null) {
    if (template == null) template = GetDefaultTemplate();
    // ...
}

An alternative to this would be the following, but assuming code blocks are appropriately small, it's probably not any clearer:

public void Create(Template template = null) {
    var theActualTemplate = template ?? GetDefaultTemplate();
    // ...
}

I apologize for this surely tired/already-answered question, but oddly I can't find anything on it. I tried looking through some C# style guides (including all the ones here: Style guide for c#?), but didn't find this issue covered. Maybe it's a non-issue?

If you have a relatively authoritative source for this, I'd love to hear it.

Community
  • 1
  • 1
shannon
  • 8,664
  • 5
  • 44
  • 74
  • 1
    Maybe you can't find anything on the subject, because nobody has a problem with it. Contrast this to assigning to a foreach loop variable: that is so ugly that the language designers chose to make it illegal. Not so for assigning to parameters. – Kris Vandermotten Nov 20 '13 at 13:36
  • Note that you're not really changing `template` anywhere outside your method – Julián Urbano Nov 20 '13 at 13:38
  • 2
    Side note, I see the votes to close as 'opinion based', but I'd like to point out that if you feel this is opinion-based, that is the answer to my question. I'm not asking what YOU think, I'm asking if there's an established practice. If there's not, I'm capable of making my own style rule. Specifically, note that if it showed up in StyleCop or other tool, then the question WOULD be valid. The fact that it (apparently) does not, ALSO makes the question valid. – shannon Nov 20 '13 at 13:48
  • Another aside, not that I'll be too terribly irritated if it gets closed, I'll already have my answer. – shannon Nov 20 '13 at 13:51

2 Answers2

6

In your sample you are just modifying the variable inside your method scope. Since that is your local variable, your method can do with it whatever it wants - it does not impact the caller. In general it is better to use the same variable because it makes the code easier to maintain - if you create a new variable, you increase the risk of accidentally using the wrong one and get NullReferenceException.

If you would be using ref keyword only then the assignment will impact the caller.

Knaģis
  • 20,827
  • 7
  • 66
  • 80
  • Thank you, yes, this is what I was asking. I understand the (lack of) impact on the caller (specific to C# without the `ref`), but thought that perhaps it was considered bad form anyway. – shannon Nov 20 '13 at 13:44
  • Because some parameters are `ref`, I've always avoided treating ANY parameters as "my local variable". But I think I made that habit back before I realized I would almost NEVER use `ref`. – shannon Nov 20 '13 at 14:00
1

It can be useful when you need to set a default value for a reference type, because you can't really specify it like you would do for a value type.

public void Create(int templateId = 1) {
    // this will compile
    // ...
}

 public void Create(Template template = GetDefaultTemplate()) {
    // this WON'T compile
    // ...
}

Using null as a default value for a reference type can help you defining a default object for your parameter.

Réda Mattar
  • 4,361
  • 1
  • 18
  • 19
  • This isn't what hes asking, hes asking if its OK to change the value on template within the method. – Ashigore Nov 20 '13 at 13:35
  • I think this poster understood my question, but just overlooked that another way to handle it would be to create a new local variable for this assignment. – shannon Nov 20 '13 at 13:40
  • Indeed, i mainly focused on the `Template template = null` parameter. – Réda Mattar Nov 20 '13 at 13:41