DEV Community

Cover image for Mutation isn't always bad in JavaScript

Mutation isn't always bad in JavaScript

Nick Scialli (he/him) on November 29, 2021

We humans like dealing in absolutes. It's easy. Nuance is hard. Unfortunately for us, everything involves nuance. That's why we should question our...
Collapse
 
andreidascalu profile image
Andrei Dascalu • Edited

This kind of thinking is somewhat flawed.

  1. Mutation doesn't get blamed due to anyone's lack of understanding about references. it's about expectation pure and simple. If I have an object x and it's still named x then it shouldn't change. If it changes, its name should reflect it. It's not that looking at the code wouldn't tell me about the change, it's that it requires revisiting the code over and over whenever I need to answer a question about state instead of me following a simple convention that a variable name is descriptive enough.
  2. The idea that mutation is "excellent" if the mutation doesn't leave a function is just wrong. It might make it acceptable but the fact that it doesn't leave a function isn't some benefit it adds, it merely doesn't permeate side effects. That's not a value in itself.
  3. The notion that using mutating language constructs make code readable is actually one of the reasons I laugh at the concept that a language is just a tool. It's a tool alright but one that forms habits and mentality. The fact that a developer can get along with mutating language constructs because Ina certain language it provides readability (despite its downsides) is something that will carry over. It becomes a way of doing things and everyone hangs on to their ways during any transition. This particular one is problematic because it's related to the particular syntax sugar of JS which teaches developers that mutation is acceptable, but you won't fare well if you take this habit elsewhere.
Collapse
 
jwp profile image
John Peters

Objects shouldn't change without a name change? You mean like an object of me and my age or address should never change? Hasn't ever been an issue in C#, ever.

Collapse
 
andreidascalu profile image
Andrei Dascalu

Objects shouldn't change in a context where the local logic doesn't expect it to. That's the gist.
In C# you are forced to do conscious decisions about mutability so certain behaviours are expected. Rust falls in the same place.

Thread Thread
 
jwp profile image
John Peters • Edited

Yes this is exactly why mutation is never an issue in C# because the context is fully known. The same applies to Javascript. "Know thy context". Trouble happens in Javascript due to bad practices. Immutabilty works perfectly in proper context but is not a "single tool solves all" where frameworks require it.

Thread Thread
 
andreidascalu profile image
Andrei Dascalu

Sorry but it's not at all the same thing. JavaScript doesn't provide immutability mechanisms at all. In C# you can specify "readonly" on object property and go to bed. In Rust you actually need to allow mutation specifically with "mut". In JavaScript you need to re-investigate the context, as anyone might inadvrtedly call the wrong function on the wrong thing. Better leave a whole lot of documentation on which functions use mutating constructs and end up mutating stuff themselves and hope everyone does flawless code review.

Thread Thread
 
jwp profile image
John Peters

You missed my point which ultimately was that the immutable fan club became that way due to Javascript programmers not knowing context. It is a 10 pound hammer for a 1 pound problem. So much so the magical spread operators were invented that allow mutation and return a new array. Over engineering in my opinion.

Collapse
 
jwp profile image
John Peters

I attribute the 'don't mutate gospel' to inexperience. It was accepted at face value in Js community solely because of opinionated frameworks like React.
So much so, that we now see entire component designs requiring its use.

In a poll here on Dev.to, I asked for comments on the goodness on non mutable design. The best answer was 'to avoid per element comparisons' to determine what changed in an array'. All other answers were weak.

We got by just fine mutating objects in C# for 30 years. So the new found non mutable crowd has little history to stand on.

Also JS didn't really have strong typing nor did users really understand by reference and by value. All contributed to traps.

It's all about perspective and experience. Either way works, but what I choose in the end is simply the right thing for my project.

This article is greatl!

Collapse
 
nas5w profile image
Nick Scialli (he/him)

I really appreciate this perspective, John. As I mentioned in the introduction, we often find ourselves clinging to dogmatic views based on some bad experiences or simply because nuance is hard.

I also think the “opinionated frameworks like React” part is more like a misunderstanding a lot of folks have about React. The truth is React works based on referential equality, so its only opinion on mutation is that you shouldn’t mutate an object if you expect React to detect that change and take an action based off of it.

Anyways, thank you so much for the thoughtful response!

Collapse
 
jwp profile image
John Peters

My thoughts on React being opinionated is based on how it demands its own way of doing things. For example just look at how it did styling. React is 20 times less opinionated than Angular which is good. The advent of webassembly brings all discussions like these to an end. We now have the ability to write entire websites using c# and it's huge set of class libraries. Cool

Collapse
 
jwp profile image
John Peters

Object reuse as a pattern of shape for mutation is just ridiculous. It violates SRP and is always by reference. Thus the birth on non mutable to me was based on malpractice all along.

When a framework dislikes creation of new objects and instead tries to copy and change, it has a fundamental issue in allowing it to happen without warning. Newbies fall on this trap often.

 
jwp profile image
John Peters

No what I meant was 25 years of programming experience with no issues with mutating objects speaks volumes.

All the non mutable stuff has only 1 great attribute; which is simply a comparison of two references to instantly detect change.

This same thing has been possible for more than 25 years now in .Net

Collapse
 
hareom284 profile image
Harry

You are explaination is save alot of time in debugging when I work in JS.This artical is freaking cool.

Collapse
 
nas5w profile image
Nick Scialli (he/him)

Thank you, I really appreciate that!

 
jwp profile image
John Peters • Edited

Ok list some advantages to immutabilty.

As for Haskell is it in top 5 or 10?

 
jwp profile image
John Peters • Edited

The 7 articles shown are not absolute truths but are presented that way. Total hype.

Collapse
 
therakeshpurohit profile image
Rakesh Purohit

I prefer not to mutate the object in any case.