2

None of my team's programs care about multithreading, parallelisation, async, or anything else that benefit from immutability. However, immutability is clearly in fashion at the moment and I'm starting to believe that it assists readability. We typically use C#, but I don't think that's relevant.

If in a code review I find that a colleague has used a collection that was more mutable than it needed to be, should I point it out? For example what if I find they've used a List where an ImmutableList or ReadOnlyList would have done?

My own thoughts on this matter are confused. On one hand, there's the two benefits that I mentioned earlier. On the other, I can only claim that code is "overly mutable" if I can guess ahead of time that the code will never need more mutability and it's tough to defend any claims that give only marginal benefits in readability.

J. Mini
  • 1,007
  • 8
  • 21
  • 3
  • A huge advantage of using something like ReadOnlyList is that it encapsulates the data and makes your intentions clear: the collection is only to be manipulated, if at all, by the creating entity, and it is not intended to be modified by outside classes or users. – user949300 Apr 14 '23 at 22:27
  • "immutability is clearly in fashion at the moment" - I would never let "fashion" dictate. The problem is that object-oriented programming is about passing pointers around, whereas the modern preference is to pass around values. This reaches the pinnacle of contradiction in the likes of C#, where the list is an object passed by reference but the contents (if they are "value types") are not, leading to a lack of clarity about whether data is local or shared. (1/2) – Steve Apr 15 '23 at 09:51
  • I doubt, however, whether the risk is worth your breath in an individual code review, and it can promote fussiness amongst individual reviewers who latch onto their own pet hates and perceptions of fashion, rather than reviewing whether the code actually contains real faults, or at least fails to meet a normal standard. I wouldn't go as far as to say a person using List is failing to meet a normal standard. The best solution to this is at the level of language design (i.e. above our heads). If there is interest within a particular shop, then issue a memo or hold a presentation instead. (2/2) – Steve Apr 15 '23 at 09:53
  • Does anyone have any suggestions for how to edit this question in to something acceptable? The up votes and comments show a clear interest. – J. Mini Apr 15 '23 at 10:26
  • @J.Mini, it seems a reasonable question. It was closed for "lack of focus", but without feedback it's difficult to discern how the question falls beneath a reasonable standard of focus. Perhaps strike the final "any cost/benefits" paragraph? Beyond that, perhaps ask for further feedback on the Meta site. – Steve Apr 15 '23 at 20:08
  • The question is effectively "what should I be a stickler for?", which is plainly a subjective question. – Flater Apr 17 '23 at 00:24

0 Answers0