Refactoring 008 - Variables That Never Change Should Be Constants

Written by mcsee | Published 2026/03/30
Tech Story Tags: programming | clean-code | technology | software-development | software-engineering | mutation | refactoring-008 | hackernoon-top-story

TLDRBe explicit on what mutates and what doesn't.via the TL;DR App

If I see a Variable that doesn't change. I call that variable a constant

TL;DR: Be explicit on what mutates and what doesn't.

Problems Addressed ๐Ÿ˜”

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxii?embedable=true

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxvi?embedable=true

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxiv?embedable=true

Context ๐Ÿ’ฌ

Variables that never change their value are not really variables.

Theyโ€™re constants pretending to be mutable state.

When we declare something as a variable, we tell the reader (and the compiler) to expect change.

If that change never happens, weโ€™re sending misleading signals about what the code actually does.

Converting these to constants shrinks the state space a developer must track.

A constant signals that a value wonโ€™t change, preventing accidental reassignments and letting the compiler optimize better.

Itโ€™s honest: if something shouldnโ€™t mutate, donโ€™t let it.

Steps ๐Ÿ‘ฃ

  1. Find the scope of the variable
  2. Define a constant with the same scope
  3. Replace the variable

Sample Code ๐Ÿ’ป

Before ๐Ÿšจ

let lightSpeed = 300000;
var gravity = 9.8;

// 1. Find the scope of the variable
// 2. Define a constant with the same scope
// 3. Replace the variable

After ๐Ÿ‘‰

const lightSpeed = 300000;
const gravity = 9.8;

// 1. Find the scope of the variable
// 2. Define a constant with the same scope
// 3. Replace the variable 

// If the object is compound, 
// we might need Object.freeze(gravity);

Type ๐Ÿ“

[X] Automatic

IDEs can check if a variable is written but never updated.

Safety ๐Ÿ›ก๏ธ

This is a safe refactoring.

Why is the Code Better? โœจ

Code is more compact and declares intent clearly.

Take it further with language-specific operators likeย const,ย final, orย let.

The scope becomes obvious at a glance.

How Does it Improve the Bijection? ๐Ÿ—บ๏ธ

This refactoring improvesย bijectionย by making it clear what mutates and what doesn't.

In the real world, most values don't change. They're constants.

Declaring them as variables createsย couplingย between what we're thinking and how we wrote it.

Constants remove that gap and align the code with the actual domain.

Tags ๐Ÿท๏ธ

  • Mutability

Level ๐Ÿ”‹

[X] Beginner

https://hackernoon.com/improving-the-code-one-line-at-a-time?embedable=true

Refactor with AI ๐Ÿค–

Suggested Prompt: 1. Find the scope of the variable.2. Define a constant with the same scope.3. Replace the variable

Without Proper Instructions ๐Ÿ“ต

With Specific Instructions ๐Ÿ‘ฉโ€๐Ÿซ

See also ๐Ÿ“š

https://hackernoon.com/is-it-crystal-clear-for-everybody-that-a-date-should-not-mutate-wuoy3z03?embedable=true


This article is part of the Refactoring Series

https://maximilianocontieri.com/how-to-improve-your-code-with-easy-refactorings?embedable=true

https://maximilianocontieri.com/how-to-improve-your-code-with-easy-refactorings


Written by mcsee | Iโ€™m a sr software engineer specialized in Clean Code, Design and TDD Book "Clean Code Cookbook" 500+ articles written
Published by HackerNoon on 2026/03/30