1

I have a large amount of code that is dependent on a list of objects. the list is modified a lot while being passed around as a parameter to various methods.

Even though I understand the workings of this code, I feel uneasy letting such an easy opportunity to make a mistake exist. Is there a way to handle this situation in c# outside of a goofy comment or refactoring?

2c2c
  • 4,694
  • 7
  • 22
  • 33
  • Is the list being modified by other threads? Otherwise, what's the problem if the list changes from a method to another? – Paolo Tedesco Jul 18 '15 at 04:49
  • I guess it may be a cultural issue based on language background... A function in an ideal world should not modify its parameters. This should be a safely assumable precondition (imo). It isn't being modified in other threads though. – 2c2c Jul 18 '15 at 04:55
  • see Eric's response http://stackoverflow.com/a/3266579/148671 – Haseeb Asif Jul 18 '15 at 05:10
  • one dirty way could be to clone the list before sending it as param to method – Haseeb Asif Jul 18 '15 at 05:11
  • Why do you not want to refactor? Seems like it is exactly what you want to do. – Ferdinand Swaters Jul 18 '15 at 05:25
  • I don't think he wants the list to be immutable, or to have a copy of the list in its original state, but just marking explicitly that the list mutates a lot... – Paolo Tedesco Jul 18 '15 at 05:26
  • think about `const Type& foo` vs `Type& foo` in C++ and how the absense of `const` is a red-flag that the parameter could be modified. in c# because nothing is ever defined as mutable-immutable it's not immediately clear. I was wondering if the feature did infact exist, though. – 2c2c Jul 18 '15 at 05:36
  • @2c2c: when I started programming in C#, coming from C++, I, too, missed the const modifier a lot :) The bottom line, however, is that in C# there's no such a thing. – Paolo Tedesco Jul 18 '15 at 05:38
  • I know you said you don't want to refactor, but what about using `ImmutableList` https://msdn.microsoft.com/en-us/library/system.collections.immutable.immutablelist(v=vs.111).aspx – 3dd Jul 18 '15 at 06:05
  • Are you mutating the state of the list itself, or the state of the individual items in the list? Is your code performance critical? How memory constrained are you? – Preston Guillot Jul 18 '15 at 16:09
  • Items are popped off as it passes through different methods. Run run time performance not an issue. Breaking on the other hand would be unfortunate – 2c2c Jul 18 '15 at 20:30

2 Answers2

2

If you are passing a List<Something> around in your code, then it is "mutable" by default, and there is no way to signal this explicitly.

If this is a language background issue (Haskell?), then in C# you should looks things from a different perspective: if you wanted to pass around an immutable collection, you would need to use some different type (maybe an IEnumerable<Something>, even if it's not the same as a list); if you're passing around a List, instead, it can be modified by every method that receives it.

Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
0

Maybe you can give that list a special type:

class MyCustomMutableList : List<int>

You could even not give it any base class to make sure that any usage site must use this special type in order to be able to access list data.

I would normally consider this a misuse of inheritance. If this is an implementation detail and does not leak out to consumers of your API it's probably good enough. Otherwise, create an IList<int> derived class through composition. R# has a feature to delegate all virtual methods to an instance field. That generates all that code.

You also could create a wrapper class that just exposes the required methods to perform the required mutations:

class DataCollector {
 public void Add(int item) { ... }
}

Since all this object allows to do is mutation it is pretty clear that mutation is going on.

usr
  • 168,620
  • 35
  • 240
  • 369