Variable reuse is something we see in big chunks of code.
TL;DR: Don't reuse variable names. You break readability and refactor chances and gain nothing but laziness.
Garbage Collection Missed Opportunities
Define, use and dispose variables.
Keep your Definition, Usage and Destroy variables short.
class Item: def __init__(self, name): self.name = name def taxesCharged(self): return 1; class Money: pass lastPurchase = Item('Soda'); # Do something with the purchase taxAmount = lastPurchase.taxesCharged(); # Lots of stuff related to the purchase # I drink the soda # I cannot extract method from below without passing # useless lastPurchase as parameter # a few hours later.. lastPurchase = Item('Whisky'); # I bough another drink taxAmount += lastPurchase.taxesCharged();
class Item: def __init__(self, name): self.name = name def taxesCharged(self): return 1; class Money: pass def buySupper(): supperPurchase = Item('Soda'); # Do something with the purchase # Lots of stuff related to the purchase # I drink the soda return supperPurchase; def buyDrinks(): # I could extract method! # a few hours later.. drinksPurchase = Item('Whisky'); # I bough another drink return drinksPurchase; taxAmount = buySupper().taxesCharged() + buyDrinks().taxesCharged();
Many linters can warn us from reusing variables
Reusing variables is a non-contextual copy and paste hint.
Code Smell 03 - Functions Are Too Long
Maxi Contieri ⭐⭐⭐ ・ Oct 22 '20 ・ 1 min read
Photo by Robby McCullough on Unsplash
Vaclav Prokop@meetvashek@mcsee1 Code smell: Reassigning variables within a single scope!
I thougt this practice is dead and now I see it again!06:21 AM - 13 Jul 2021
Either way you look at it (DRY or laziness), the idea is the same: make your program flexible. When change comes (and it always does), you'll have a much easier time changing with it.
Software Engineering Great Quotes
Maxi Contieri ⭐⭐⭐ ・ Dec 28 '20 ・ 13 min read
This article is part of the CodeSmell Series.
How to Find the Stinky parts of your Code
Maxi Contieri ⭐⭐⭐ ・ May 21 '21 ・ 8 min read
Last update: 2021/07/14
Top comments (0)