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

前端 未结 30 1102
夕颜
夕颜 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:30

    I would go for Add, because I can see the benefit of a better name, but the problem would be to find different names for every other immutable operation which might make the class quite unfamiliar if that makes sense.

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

    I think the key thing you're trying to get at that's hard to express is the nonpermutation, so maybe something with a generative word in it, something like CopyWith() or InstancePlus().

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

    I don't think the English language will let you imply immutability in an unmistakable way while using a verb that means the same thing as "Add". "Plus" almost does it, but people can still make the mistake.

    The only way you're going to prevent your users from mistaking the object for something mutable is by making it explicit, either through the name of the object itself or through the name of the method (as with the verbose options like "GetCopyWith" or "CopyAndAdd").

    So just go with your favourite, "Plus."

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

    I think "Add" or "Plus" sounds fine. The name of the list itself should be enough to convey the list's immutability.

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

    Maybe there are some words which remember me more of making a copy and add stuff to that instead of mutating the instance (like "Concatenate"). But i think having some symmetry for those words for other actions would be good to have too. I don't know of a similar word for "Remove" that i think of the same kind like "Concatenate". "Plus" sounds little strange to me. I wouldn't expect it being used in a non-numerical context. But that could aswell come from my non-english background.

    Maybe i would use this scheme

    AddToCopy
    RemoveFromCopy
    InsertIntoCopy
    

    These have their own problems though, when i think about it. One could think they remove something or add something to an argument given. Not sure about it at all. Those words do not play nice in chaining either, i think. Too wordy to type.

    Maybe i would just use plain "Add" and friends too. I like how it is used in math

    Add 1 to 2 and you get 3
    

    Well, certainly, a 2 remains a 2 and you get a new number. This is about two numbers and not about a list and an element, but i think it has some analogy. In my opinion, add does not necessarily mean you mutate something. I certainly see your point that having a lonely statement containing just an add and not using the returned new object does not look buggy. But I've now also thought some time about that idea of using another name than "add" but i just can't come up with another name, without making me think "hmm, i would need to look at the documentation to know what it is about" because its name differs from what I would expect to be called "add". Just some weird thought about this from litb, not sure it makes sense at all :)

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

    Append - because, note that names of the System.String methods suggest that they mutate the instance, but they don't.

    Or I quite like AfterAppending:

    void test()
    {
      Bar bar = new Bar();
      List list = bar.AfterAppending("foo");
    }
    
    0 讨论(0)
提交回复
热议问题