Code Smell 176 — Changes in Essence

Code Smell 176 — Changes in EssenceMutation is good. Things changeTL;DR: Don’t change essential attributes or behaviorProblemsBijection violationMutabilityRipple EffectSolutionsProtect essential attributes from change.Remove settersRefactoringsRefactor…


This content originally appeared on Level Up Coding - Medium and was authored by Maximiliano Contieri

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

Refactoring 001 — Remove Setters

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

Code Smell 16 — Ripple Effect

More Info

The evil powers of mutants

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

Software Engineering Great Quotes

This article is part of the CodeSmell Series.

How to Find the Stinky parts of your Code


Code Smell 176 — Changes in Essence was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Maximiliano Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2022-11-03T13:02:54+00:00) Code Smell 176 — Changes in Essence. Retrieved from https://www.scien.cx/2022/11/03/code-smell-176-changes-in-essence/

MLA
" » Code Smell 176 — Changes in Essence." Maximiliano Contieri | Sciencx - Thursday November 3, 2022, https://www.scien.cx/2022/11/03/code-smell-176-changes-in-essence/
HARVARD
Maximiliano Contieri | Sciencx Thursday November 3, 2022 » Code Smell 176 — Changes in Essence., viewed ,<https://www.scien.cx/2022/11/03/code-smell-176-changes-in-essence/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Code Smell 176 — Changes in Essence. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/11/03/code-smell-176-changes-in-essence/
CHICAGO
" » Code Smell 176 — Changes in Essence." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2022/11/03/code-smell-176-changes-in-essence/
IEEE
" » Code Smell 176 — Changes in Essence." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2022/11/03/code-smell-176-changes-in-essence/. [Accessed: ]
rf:citation
» Code Smell 176 — Changes in Essence | Maximiliano Contieri | Sciencx | https://www.scien.cx/2022/11/03/code-smell-176-changes-in-essence/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.