Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I ended up going with Add for all of my Immutable Collections in <a href="http://code.msdn.microsoft.com/BclExtras" rel="noreferrer">BclExtras</a>. The reason being is that it's an easy predictable name. I'm not worried so much about people confusing Add with a mutating add since the name of the type is prefixed with Immutable.</p> <p>For awhile I considered Cons and other functional style names. Eventually I discounted them because they're not nearly as well known. Sure functional programmers will understand but they're not the majority of users.</p> <p>Other Names: you mentioned:</p> <ul> <li>Plus: I'm wishy/washing on this one. For me this doesn't distinguish it as being a non-mutating operation anymore than Add does</li> <li>With: Will cause issues with VB (pun intended)</li> <li>Operator overloading: Discoverability would be an issue</li> </ul> <p>Options I considered:</p> <ul> <li>Concat: String's are Immutable and use this. Unfortunately it's only really good for adding to the end</li> <li>CopyAdd: Copy what? The source, the list? </li> <li>AddToNewList: Maybe a good one for List. But what about a Collection, Stack, Queue, etc ...</li> </ul> <p>Unfortunately there doesn't really seem to be a word that is </p> <ol> <li>Definitely an immutable operation</li> <li>Understandable to the majority of users</li> <li>Representable in less than 4 words </li> </ol> <p>It gets even more odd when you consider collections other than List. Take for instance Stack. Even first year programmers can tell you that Stacks have a Push/Pop pair of methods. If you create an ImmutableStack and give it a completely different name, lets call it Foo/Fop, you've just added more work for them to use your collection. </p> <p><strong>Edit:</strong> Response to Plus Edit</p> <p>I see where you're going with Plus. I think a stronger case would actually be Minus for remove. If I saw the following I would certainly wonder what in the world the programmer was thinking</p> <pre><code>list.Minus(obj); </code></pre> <p>The biggest problem I have with Plus/Minus or a new pairing is it feels like overkill. The collection itself already has a distinguishing name, the Immutable prefix. Why go further by adding vocabulary whose intent is to add the same distinction as the Immutable prefix already did.</p> <p>I can see the call site argument. It makes it clearer from the standpoint of a single expression. But in the context of the entire function it seems unnecessary.</p> <p><strong>Edit 2</strong></p> <p>Agree that people have definitely been confused by String.Concat and DateTime.Add. I've seen several very bright programmers hit this problem. </p> <p>However I think ImmutableList is a different argument. There is nothing about String or DateTime that establishes it as Immutable to a programmer. You must simply <em>know</em> that it's immutable via some other source. So the confusion is not unexpected. </p> <p>ImmutableList does not have that problem because the name defines it's behavior. You could argue that people don't know what Immutable is and I think that's also valid. I certainly didn't know it till about year 2 in college. But you have the same issue with whatever name you choose instead of Add. </p> <p><strong>Edit 3:</strong> What about types like TestSuite which are immutable but do not contain the word?</p> <p>I think this drives home the idea that you shouldn't be inventing new method names. Namely because there is clearly a drive to make types immutable in order to facilitate parallel operations. If you focus on changing the name of methods for collections, the next step will be the mutating method names on every type you use that is immutable. </p> <p>I think it would be a more valuable effort to instead focus on making types identifiable as Immutable. That way you can solve the problem without rethinking every mutating method pattern out there. </p> <p>Now how can you identify TestSuite as Immutable? In todays environment I think there are a few ways</p> <ol> <li>Prefix with Immutable: ImmutableTestSuite</li> <li>Add an Attribute which describes the level of Immutablitiy. This is certainly less discoverable</li> <li>Not much else.</li> </ol> <p>My guess/hope is development tools will start helping this problem by making it easy to identify immutable types simply by sight (different color, stronger font, etc ...). But I think that's the answer though over changing all of the method names. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload