[...] because every operation that relies on m
should always account for the possibility of it being empty
Finding the right names for things can be a challenge. Making assumptions can sometimes lead to trouble.
As a pragmatic developer, I understand that there are situations where mutation is necessary. However, it is important to recognize the potential risks and communicate them to others so they can make informed decisions.
Once during a code review, I noticed that a function was modifying its parameter in a way that could cause issues:
function foo(bar) {
const baz = {...bar};
baz.a.b.c = 10;
return baz;
}
The author of the function defended their approach by claiming they had cloned the object beforehand, making the function 'pure'.
If I hadn't taken the time to discuss this with them, we might have encountered a major problem in production. It turned out that the application state was being mutated, leading to false positive test results.
In my opinion, confusion is one of the worst outcomes of mutating data.
Tracking bugs caused by mutation can be quite challenging.
I always advise my team members not to overcomplicate their code by trying to cover every "impossible case." This often leads to unnecessary checks and hampers our confidence in the codebase.
However, unpredictable scenarios can arise if there is uncontrolled access to data.
I have witnessed instances where people unknowingly mutate data. When working with team members of varying experience levels, it's essential to:
- Avoid making assumptions
- Educate others
- Utilize a library that enforces immutability
This may not be the textbook answer you were expecting, but I hope these tips are helpful.