DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Code Smell 176 - Changes in Essence
Maxi Contieri ⭐⭐⭐
Maxi Contieri ⭐⭐⭐

Posted on • Originally published at maximilianocontieri.com

Code Smell 176 - Changes in Essence

Mutation is good. Things change

TL;DR: Don't change essential attributes or behavior

Problems

Solutions

  1. Protect essential attributes from change.

  2. Remove setters

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);
Enter fullscreen mode Exit fullscreen mode

Right

const date = new Date("2022-03-25");
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)

Create an Account! The only reason people scroll to the bottom... Β 
is because they want to read more.

Create an account to bookmark, comment, and react to articles that interest you.