Code Smell 158 - Variables not Variable

Photo by Noah Buscher on Unsplash

Code Smell 158 - Variables not Variable

You assign a value to a variable and use it, but never change it

Maxi Contieri
·Aug 13, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Problems
  • Solutions
  • Context
  • Sample Code
  • Detection
  • Tags
  • Conclusion
  • Relations
  • Refactorings
  • More Info
  • Disclaimer
  • Credits

TL;DR: Be declarative on mutability.


  • Readability

  • Honor the Bijection mutability.

  • Potential performance and memory issues.


  1. Change the variable to a constant and be clear on its scope


We are always learning from the domain.

Sometimes we guess that a value can change with the MAPPER.

Later on, we learn it won't change.

Therefore we need to promote it to a constant.

This will also avoid Magic Constants

Sample Code



function configureUser() {
  $password = '123456';
  // Setting a password on a variable is another vulnerability
  // And Code Smell
  $user = new User($password);



define("USER_PASSWORD", '123456')

function configureUser() {  
  $user = new User(USER_PASSWORD);

// or 

function configureUser() {  
  $user = new User(userPassword());

function userPassword() : string {
  return '123456';

// Case is an oversimplification as usual


[X] Automatic

Many linters check if the variable has just one assignment.

We can also perform mutation testing and try to modify the variable to see if tests break.


  • Mutability


We must challenge ourselves and refactor when the variable scope is clear and we learn more about its properties and mutability.



More Info


Code Smells are just my opinion.


Photo by Noah Buscher on Unsplash

A complex system that works is invariably found to have evolved from a simple system that worked.

John Gall

This article is part of the CodeSmell Series.

Share this


Technical Opinions are my own. I don't have the revealed truth.

Software Design is a creative activity. These are hints and not rigid rules.

I write on BackEnd Business Systems and OOP Design. My advice/experience might not suit other systems.

You can write me at info(at)