# Code Smell 158 - Variables not Variable

> TL;DR: Be declarative on mutability.

# Problems

- Readability

- Honor the [Bijection](https://maximilianocontieri.com/the-one-and-only-software-design-principle) mutability.

- Potential performance and memory issues. 

# Solutions

1. Change the [variable to a constant](https://maximilianocontieri.com/refactoring-008-convert-variables-to-constant) and be clear on its scope

# Context

We are always learning from the domain.

Sometimes we guess that a value can change with the [MAPPER](https://maximilianocontieri.com/what-is-wrong-with-software).

Later on, we learn it won't change.

Therefore we need to promote it to a constant.

This will also avoid [Magic Constants](https://maximilianocontieri.com/code-smell-02-constants-and-magic-numbers)

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/ab8aca666d5064bac5a4e8d096900138)
```php
<?php

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

## Right

[Gist Url]: # (https://gist.github.com/mcsee/9649878302b3d66603b2cfdce2de87ba)
```php
<?php

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
```

# Detection

[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.

# Tags

- Mutability

# Conclusion

We must challenge ourselves and refactor when the variable scope is clear and we learn more about its properties and [mutability](https://maximilianocontieri.com/the-evil-powers-of-mutants). 

# Relations

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

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

%[https://maximilianocontieri.com/code-smell-107-variables-reuse]

%[https://maximilianocontieri.com/code-smell-02-constants-and-magic-numbers]

# Refactorings

%[https://maximilianocontieri.com/refactoring-003-extract-constant]

%[https://maximilianocontieri.com/refactoring-008-convert-variables-to-constant]

# More Info

%[https://maximilianocontieri.com/the-evil-powers-of-mutants]

# Disclaimer

Code Smells are just my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Noah Buscher](https://unsplash.com/@noahbuscher) on [Unsplash](https://unsplash.com/s/photos/tied)  

* * *

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

_John Gall_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
