An Interest In:
Web News this Week
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
- April 13, 2024
August 25, 2021 03:25 am GMT
Original Link: https://dev.to/mcsee/code-smell-86-mutable-const-arrays-46ag
Code Smell 86 - Mutable Const Arrays
Const declares something to be constant. Can it mutate?
TL;DR: Don't rely on languages cheating about directives.
Problems
Unexpected side effects.
Accidental complexity.
Solutions
Use better languages
Use spread operator
Sample Code
Wrong
const array = [1, 2];array.push(3)//array => [1, 2, 3]//Wasn't it constant ?//constant != immutable ?
Right
const array = [1, 2];const newArray = [...array,3 ]//array => [1, 2] Didn't mutate//newArray = [1, 2, 3]
Detection
Since this is a "language feature", we can explicitly forbid it.
Tags
Mutability
JavaScript
Conclusion
We should always favour immutability on our designs and take extra care with side effects.
More Info
Credits
Thank you, @oliverjumpertz for this tip.
Maxi Contieri@mcsee113:06 PM - 24 Aug 2021
Correctness is clearly the prime quality. If a system does not do what it is supposed to do, then everything else about it matters little.
Bertrand Meyer
Software Engineering Great Quotes
Maxi Contieri Dec 28 '20 13 min read
#codenewbie #programming #quotes #software
This article is part of the CodeSmell Series.
Original Link: https://dev.to/mcsee/code-smell-86-mutable-const-arrays-46ag
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To