paint-brush
Code Smell 240 - Dead Store Code  by@mcsee
122 reads

Code Smell 240 - Dead Store Code

by Maximiliano ContieriFebruary 4th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Avoid the dead store code smell by refraining from assigning and immediately overwriting values. This article highlights readability and efficiency problems associated with dead code, offering solutions like removing unnecessary statements. Understand the impact on code quality and learn detection methods, emphasizing the importance of regular code reviews and good programming practices.
featured image - Code Smell 240 - Dead Store Code
Maximiliano Contieri HackerNoon profile picture

POV: 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

<?

$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

<?

$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 can find this problem using

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.


Code Smell 09 - Dead Code

Code Smell 54 - Anchor Boats


More Info

Disclaimer: Code Smells are my opinion.

Credits

Photo by on

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

Francis Glassborow


This article is part of the CodeSmell Series—How to Find the Stinky Parts of your Code


바카라사이트 바카라사이트 온라인바카라