After testing and reading the doc about the collection methods. I realize that some modify the original collection (mutable) and some not (immutable).
Why this technical choice?
I have issues understanding this decision.
Let’s take an example.
if we have $col1 and do a sort like that :
$col2 and $col1 will share the same reference. What is the purpose to return a collection if the original collection is also modified? We end up with 2 collections that are actually the same.
It could be nicer, to not modify the original collection so that we will still have the original collection before the sort, and the modified one.
Collection methods, in my opinion, should always be immutable and return the result as a new reference. (https://medium.com/dailyjs/the-state-of-immutability-169d2cd11310)
If we want to have only one collection, we can easily do
But in the current situation, if we want to keep the original collection not sorted we must do
So we end up with 3 collections : 1 not sorted and 2 sorted that share the same reference.
So why this choice to do some methods mutable and some immutable? Is mutable methods dangerous and can have side effect if we are not careful?
It is also hard to remember which method are mutable and which are not and we have to be careful because the 2 behaviour doesn’t need the same treatment in the code.