Code Smell 86 — Mutable Const Arrays

Maximiliano Contieri
Dev Genius
Published in
1 min readAug 25, 2021
Photo by Zorik D on Unsplash

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

  1. Use better languages
  2. Use spread operator

Sample Code

Wrong

Right

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,Oliver Jumpertz for this tip.

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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Maximiliano Contieri

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com

Responses (1)

What are your thoughts?