Code Smell 240 - Dead Store
Tags: beginners, programming, tutorial, php
Published: 2024-02-04 15:07:15

*You assign a value and overwrite it immediately*

> TL;DR: Don't Assign and overwrite values

# Problems

- Readability

- Dead Code

- Inefficiency

# Solutions

1. Remove the sentences that have no effect.

# Context

The "dead store" code smell refers to a situation in programming where a variable is assigned a value but is never subsequently read or used in the program.

it's a variable that is given a value but that value is never utilized, making the assignment unnecessary and potentially indicative of a mistake or inefficiency in the code.

This code smell can arise for various reasons, including during refactoring or changes in the program logic over time. 

Unused variables may clutter the code, making it harder to understand and potentially impacting performance.

# Sample Code

## Wrong

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

$lastGoalAuthor = "Lio Messi";
$lastGoalAuthor = "Ángel Di María";
$lastGoalAuthor = "Lio Messi";

// This is stored unconditionally 
// You can optimize it by removing the first two statements
// Since storing in a variable has no side effects
```

## Right

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

$lastGoalAuthor = "Lio Messi";
// If you want to keep the last one

$goalAuthors[] = "Lio Messi";
$goalAuthors[] = "Ángel Di María";
$goalAuthors[] = "Lio Messi";
// If you want to keep a list
  
$lastGoalAuthor = firstGoalAutor();
$lastGoalAuthor = secondGoalAutor();
$lastGoalAuthor = thirdGoalAutor();

// This might be valid since functions in
// Object-Oriented Programming might have side effects
// and you cannot remove the first ones 
// Unless you ensure they don't have side effects  
```

# Detection

[X] Automatic 

Several [linters](https://rules.sonarsource.com/php/type/Bug/RSPEC-4143/) can find this problem using [ASTs](https://en.wikipedia.org/wiki/Abstract_syntax_tree)

# Exceptions

- You should not optimize functions with side effects 

# Tags

- Bloaters

# Level

[X] Beginner

# AI Assistants

Code generated by AIs usually doesn't have this problem.

# Conclusion			  

Avoiding dead store code helps improve code quality, maintainability, and resource efficiency. 

It contributes to a more understandable, robust, and bug-free codebase.

Regular code reviews, static analysis tools, and good programming practices can aid in identifying and addressing dead store code smells.

# Relations

{% post https://dev.to/mcsee/code-smell-09-dead-code-3ig2 %}

{% post https://dev.to/mcsee/code-smell-54-anchor-boats-5g25 %}

# More Info

[Sonar Source Rule](https://rules.sonarsource.com/php/type/Bug/RSPEC-4143/)

# Disclaimer

Code Smells are my [opinion](https://dev.to/mcsee/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned-1n3a).

# Credits
	     
Photo by [Brayan Becerra](https://unsplash.com/@bryanjose23) on [Unsplash](https://unsplash.com/photos/a-building-with-a-fence-around-it--A_8VYIipNc)
         
* * *

> Good programmers use their brains, but good guidelines save us having to think out every case.

_Francis Glassborow_ 
 
{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}