Code Smell 46 — Repeated Code
DRY is our mantra. Teachers tell us to remove duplication. We need to go beyond.
DRY is our mantra. Teachers tell us to remove duplication. We need to go beyond.

Problems
Code Duplication
Maintainability
Solutions
Find repeated patterns (not repeated code).
Create an abstraction.
Parametrize abstraction calls.
Use composition and never inheritance.
Unit test new abstraction.
Sample Code
Wrong
Right
Detection
Linters can find repeated code.
There are not very good finding similar patterns.
Maybe soon machine learning will help us find such abstractions automatically.
For now, it is up to us, humans.
Examples
Tags
Duplication
Conclusion
Repeated code is always a smell.
Copying and pasting code is always a shame.
With our refactoring tools, we need to accept the duplication remove challenge trusting our tests as a safety net.
Relations
Code Smell 11 — Subclassification for Code Reuse
Code reuse is good. But subclassing generates a static coupling.medium.com
More info
DRY - Don't repeat yourself
The DRY principle is an integral part of clean code, but what does it actually mean and what is it really good for…deepdive.hashnode.dev
Laziness II: Code Wizards
Code generators do our hard work. But we don’t need them anymore.codeburst.io
Copy and paste is a design error.
David Parnas
Software Engineering Great Quotes
Sometimes a short thought can bring amazing ideas.medium.com
This article is part of the CodeSmell Series.
How to Find the Stinky parts of your Code
The code smells bad. Let’s see how to change the aromas.medium.com