DEV Community

Cover image for Code Smell 127 - Mutable Constants
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

5 1

Code Smell 127 - Mutable Constants

You declare something a constant. But you can mutate it.

TL;DR: Use inmutable constants

Problems

  • Mutability

  • The Least Surprise Principle violation

  • Coupling

Solutions

  1. Enforce mutability

  2. Avoid constants. They are hard to mock in tests.

Context

We learned to declare constants in our first course on computer programming.

As always, it is not important if something is constant.

It is important if it does not mutate.

Sample Code

Wrong

const DISCOUNT_PLATINUM = 0.1;
const DISCOUNT_GOLD = 0.05;
const DISCOUNT_SILVER = 0.02;

//Since variables are constants we cannot reassign them
const DISCOUNT_PLATINUM = 0.05; //Error

//We can group them
const ALL_CONSTANTS = {
  DISCOUNT: {
    PLATINUM = 0.1;
    GOLD = 0.04;
    SILVER = 0.02;  
  },
};

const ALL_CONSTANTS = 3.14; //Error

ALL_CONSTANTS.DISCOUNT.PLATINUM = 0.08; //NOT AN ERROR. WTF!


const ALL_CONSTANTS = Object.freeze({
  DISCOUNT: 
    PLATINUM = 0.1;
    GOLD = 0.05;
    SILVER = 0.02; 
});

const ALL_CONSTANTS = 3.14; //Error

ALL_CONSTANTS.DISCOUNT.PLATINUM = 0.12; //NOT AN ERROR. WTF!
Enter fullscreen mode Exit fullscreen mode

Right

export const ALL_CONSTANTS = Object.freeze({
  DISCOUNT: Object.freeze({
    PLATINUM = 0.1;
    GOLD = 0.05;
    SILVER = 0.02;  
  }),
});

const ALL_CONSTANTS = 3.14; //Error

ALL_CONSTANTS.DISCOUNT.PLATINUM = 0.12; //ERROR

//Code works, but it is coupled and we cannot test it

Class TaxesProvider {
  applyPlatinum(product);
}

//Now we can couple to a interface (the protocol of taxes provider)
//Since class has no setters it is constant an immuatable
//And we can replace it on tests
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Semi-Automatic

We can perform mutation testing to find changed values.

Tags

  • Constants

Conclusion

Mutability is very important.

We need to enforce it with the right tools.

Relations

More Info

Credits

This smell was inspired by This

Photo by Sangharsh Lohakare on Unsplash


You start digging in the code. The more you dig, the more stuff you turn up. Eventually you dig yourself into a hole you can’t get out of. To avoid digging your own grave, refactoring must be done systematically.

Eric Gamma


This article is part of the CodeSmell Series.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay