What's the best name for a non-mutating “add” method on an immutable collection?

前端 未结 30 1101
夕颜
夕颜 2020-11-29 16:47

Sorry for the waffly title - if I could come up with a concise title, I wouldn\'t have to ask the question.

Suppose I have an immutable list type. It has an operat

相关标签:
30条回答
  • 2020-11-29 17:13

    Actually I like And, especially in the idiomatic way. I'd especially like it if you had a static readonly property for the Empty list, and perhaps make the constructor private so you always have to build from the empty list.

    var list = ImmutableList<string>.Empty.And("Hello")
                                          .And("Immutable")
                                          .And("Word");
    
    0 讨论(0)
  • 2020-11-29 17:13

    I ended up going with Add for all of my Immutable Collections in BclExtras. 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.

    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.

    Other Names: you mentioned:

    • 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
    • With: Will cause issues with VB (pun intended)
    • Operator overloading: Discoverability would be an issue

    Options I considered:

    • Concat: String's are Immutable and use this. Unfortunately it's only really good for adding to the end
    • CopyAdd: Copy what? The source, the list?
    • AddToNewList: Maybe a good one for List. But what about a Collection, Stack, Queue, etc ...

    Unfortunately there doesn't really seem to be a word that is

    1. Definitely an immutable operation
    2. Understandable to the majority of users
    3. Representable in less than 4 words

    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.

    Edit: Response to Plus Edit

    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

    list.Minus(obj);
    

    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.

    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.

    Edit 2

    Agree that people have definitely been confused by String.Concat and DateTime.Add. I've seen several very bright programmers hit this problem.

    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 know that it's immutable via some other source. So the confusion is not unexpected.

    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.

    Edit 3: What about types like TestSuite which are immutable but do not contain the word?

    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.

    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.

    Now how can you identify TestSuite as Immutable? In todays environment I think there are a few ways

    1. Prefix with Immutable: ImmutableTestSuite
    2. Add an Attribute which describes the level of Immutablitiy. This is certainly less discoverable
    3. Not much else.

    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.

    0 讨论(0)
  • 2020-11-29 17:13

    Looking at http://thesaurus.reference.com/browse/add and http://thesaurus.reference.com/browse/plus I found gain and affix but I'm not sure how much they imply non-mutation.

    0 讨论(0)
  • 2020-11-29 17:14

    Maybe the confusion stems from the fact that you want two operations in one. Why not separate them? DSL style:

    var list = new ImmutableList<string>("Hello");
    var list2 = list.Copy().With("World!");
    

    Copy would return an intermediate object, that's a mutable copy of the original list. With would return a new immutable list.

    Update:

    But, having an intermediate, mutable collection around is not a good approach. The intermediate object should be contained in the Copy operation:

    var list1 = new ImmutableList<string>("Hello");
    var list2 = list1.Copy(list => list.Add("World!"));
    

    Now, the Copy operation takes a delegate, which receives a mutable list, so that it can control the copy outcome. It can do much more than appending an element, like removing elements or sorting the list. It can also be used in the ImmutableList constructor to assemble the initial list without intermediary immutable lists.

    public ImmutableList<T> Copy(Action<IList<T>> mutate) {
      if (mutate == null) return this;
      var list = new List<T>(this);
      mutate(list);
      return new ImmutableList<T>(list);
    }
    

    Now there's no possibility of misinterpretation by the users, they will naturally fall into the pit of success.

    Yet another update:

    If you still don't like the mutable list mention, even now that it's contained, you can design a specification object, that will specify, or script, how the copy operation will transform its list. The usage will be the same:

    var list1 = new ImmutableList<string>("Hello");
    // rules is a specification object, that takes commands to run in the copied collection
    var list2 = list1.Copy(rules => rules.Append("World!"));
    

    Now you can be creative with the rules names and you can only expose the functionality that you want Copy to support, not the entire capabilities of an IList.

    For the chaining usage, you can create a reasonable constructor (which will not use chaining, of course):

    public ImmutableList(params T[] elements) ...
    
    ...
    
    var list = new ImmutableList<string>("Hello", "immutable", "World");
    

    Or use the same delegate in another constructor:

    var list = new ImmutableList<string>(rules => 
      rules
        .Append("Hello")
        .Append("immutable")
        .Append("World")
    );
    

    This assumes that the rules.Append method returns this.

    This is what it would look like with your latest example:

    var suite = new TestSuite<string, int>(x => x.Length);
    var otherSuite = suite.Copy(rules => 
      rules
        .Append(x => Int32.Parse(x))
        .Append(x => x.GetHashCode())
    );
    
    0 讨论(0)
  • 2020-11-29 17:17

    I would call it Extend() or maybe ExtendWith() if you feel like really verbose.

    Extends means adding something to something else without changing it. I think this is very relevant terminology in C# since this is similar to the concept of extension methods - they "add" a new method to a class without "touching" the class itself.

    Otherwise, if you really want to emphasize that you don't modify the original object at all, using some prefix like Get- looks like unavoidable to me.

    0 讨论(0)
  • 2020-11-29 17:17

    First, an interesting starting point: http://en.wikipedia.org/wiki/Naming_conventions_(programming) ...In particular, check the "See Also" links at the bottom.

    I'm in favor of either Plus or And, effectively equally.

    Plus and And are both math-based in etymology. As such, both connote mathematical operation; both yield an expression which reads naturally as expressions which may resolve into a value, which fits with the method having a return value. And bears additional logic connotation, but both words apply intuitively to lists. Add connotes action performed on an object, which conflicts with the method's immutable semantics.

    Both are short, which is especially important given the primitiveness of the operation. Simple, frequently-performed operations deserve shorter names.

    Expressing immutable semantics is something I prefer to do via context. That is, I'd rather simply imply that this entire block of code has a functional feel; assume everything is immutable. That might just be me, however. I prefer immutability to be the rule; if it's done, it's done a lot in the same place; mutability is the exception.

    0 讨论(0)
提交回复
热议问题