Returning self or void suggests mutability

Login Password Aphyr Blog Photography Code 2015/02/12 Ruby Software Clojure So there’s a a blog post that advises every method should, when possible, return self . I’d like to suggest you do the opposite: wherever possible, return something other than self . Mutation makes code harder to reason about. Mutable objects make equality comparisons tricky: if you use a mutable object as the key in a hashmap, for instance, then change one of its fields, what happens? Can you access the value by the new string value? By the old one? What about a set? An array? For a fun time, try these in various languages. Try it with mutable primitives, like Strings, if the language makes a distinction. Enjoy the results. If you call a function with a mutable object as an argument, you have very few guarantees about the new object’s value. It’s up to you to enforce invariants ...

Linked on 2015-02-13 21:34:50 | Similar Links