
Mutation is good. Things change
TL;DR: Don't change essential attributes or behavior
Problems
Bijection violation
Solutions
Protect essential attributes from change.
Refactorings
Context
Heraclitus said:
“No man ever steps in the same river twice. For it’s not the same river and he’s not the same man.”
The man stays the same in essence. But his body evolves.
Sample Code
Wrong
const date = new Date();
date.setMonth(4);
Right
const date = new Date("2022-03-25");
Detection
[X] Manual
This is a semantic smell. We need to model which attributes/behaviors are essential and which are accidental.
Tags
Conclusion
We need to favor immutable objects.
Objects can mutate in accidental ways, not in essential ones.
Relations
More Info
Disclaimer
Code Smells are just my opinion.
Credits
Photo by Nick Fewings on Unsplash
Changes in software design will eventually mean "one step forward, two steps back". It is inevitable.
Salman Arshad
This article is part of the CodeSmell Series.
About the author
I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.
Comments
Join the discussionNo comments yet. Be the first to comment.
Code Smells
In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.





