# Code Smell 107 - Variables Reuse

> TL;DR: Don't read and write the same variable for different purposes

# Problems

- Readability

- Hidden problems

# Solutions

1. Don't reuse variables

2. [Extract Method](https://maximilianocontieri.com/refactoring-002-extract-method) to isolate scopes

# Context

When programming a script it is common to reuse variables.

This leads to confusion and makes debugging harder.

We should narrow the scope as much as possible.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/88615884493c78d45a57be565964ae5b)
```java
// print line total
double total = item.getPrice() * item.getQuantity();
System.out.println("Line total: " + total );

// print amount total 
total = order.getTotal() - order.getDiscount();
System.out.println( "Amount due: " + total );

// variable is reused
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/9657946be3bcd5a81aebc12d4ef82d0b)
```java
function printLineTotal() {
  double total = item.getPrice() * item.getQuantity();
  System.out.println("Line total: " + total );
}

function printAmountTotal() {
  double total = order.getTotal() - order.getDiscount();
  System.out.println( "Amount due: " + total );
}
```

# Detection

[X] Automatic 

Linters can use the parse tree to find variable definition and usages.

# Tags

- Readability

# Conclusion

Avoid reusing variable names. Use more specific and different names.

# Relations

%[https://maximilianocontieri.com/code-smell-03-functions-are-too-long]

# More Info

%[https://maximilianocontieri.com/refactoring-002-extract-method]

# Credits

Photo by <a href="https://unsplash.com/@sigmund">Sigmund</a> on <a href="https://unsplash.com/s/photos/recycle">Unsplash</a>
  
* * *

> Simplicity before generality, use before reuse.

_Kevlin Henney_
 
%[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]
