Refactoring 008 - Convert Variables to Constant

Refactoring 008 - Convert Variables to Constant

Leader 1 47 113
calendar_todayschedule4 min read

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://maximilianocontieri.com/code-smell-158-variables-not-variable

https://maximilianocontieri.com/code-smell-127-mutable-constants

https://maximilianocontieri.com/code-smell-116-variables-declared-with-var

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://maximilianocontieri.com/refactoring-003-extract-constant

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
ChatGPT ChatGPT
Claude Claude
Perplexity Perplexity
Copilot Copilot
You You
Gemini Gemini
DeepSeek DeepSeek
Meta AI Meta AI
Grok Grok
Qwen Qwen

See also

https://coderlegion.com/6742/the-evil-power-of-mutants


This article is part of the Refactoring Series

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

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

Refactoring 012 - Reify Associative Arrays

Maxi Contieri - Jul 8

Refactoring 011 - Replace Comments with Tests

Maxi Contieri - Jun 17

Refactoring 010 - Extract Method Object

Maxi Contieri - Jun 9

Refactoring 009 - Protect Public Attributes

Maxi Contieri - Apr 2

Refactoring 007 - Extract Class

Maxi Contieri - Mar 22
chevron_left
6.5k Points161 Badges
Buenos Aires, Argentinamaximilianocontieri.com
67Posts
3Comments
4Connections
Learn something new every day
Software Engineer and author of Clean Code Cookbook (https://amzn.to/4... Show more

Related Jobs

View all jobs →

Commenters (This Week)

3 comments
3 comments
2 comments

Contribute meaningful comments to climb the leaderboard and earn badges!