TL;DR: You can find missing abstractions by looking at repeated code
Problems
DRY principle violation
Maintainability
Ripple Effect
Solutions
Create the repeated code
Introduce an abstraction
Replace the references and point to the new abstraction
Remove the duplication
Context
Repeated code is a symptom of missing abstractions.
This is natural in the learning process since we cannot foresee those abstractions.
Sample Code
Wrong
def calculate_area(length, width):
return length * width
def calculate_volume(length, width, height):
return length * width * height
Right
def calculate_area(length, width):
return length * width
def calculate_volume(length, width, height):
base_area = calculate_area(length, width)
return base_area * height
Detection
[X] Semi-Automatic
Some linters can find repeated code patterns.
Exceptions
The abstraction must have a dependency correspondence on the Bijection
Tags
Bloaters
Conclusion
Repeated code is a problem and a hint for a missing abstraction.
Remember you don't need to avoid copying and pasting.
You must explicitly write the repeated code and remove the duplication by introducing an abstraction.
Avoiding the cut and paste is a shortcut and a symptom of premature optimization.
Relations
Code Smell 182 - Over Generalization
Disclaimer
Code Smells are my opinion.
Credits
Photo by Mitchell Griest on Unsplash
Pasting code from the internet into production code is like chewing gum found in the street.
Mike Johnson
Software Engineering Great Quotes
This article is part of the CodeSmell Series.
Repeated code is annoying and becomes a tech liability overtime.
I think it is essential tl reiterate the code every once in a while to detect repeated code and add more abstractions.
Thank you for the short and nice advice 🙂
I have one question though. I am a bit unclear as of why you are saying that repeated code should be written and then removed and link it when writing new abstraction? Why noy just write abstraction from the start?