DEV Community

Cover image for Code Smell 46 - Repeated Code
Maxi Contieri ⭐⭐⭐
Maxi Contieri ⭐⭐⭐

Posted on • Updated on • Originally published at maximilianocontieri.com

 

Code Smell 46 - Repeated Code

DRY is our mantra. Teachers tell us to remove duplication. We need to go beyond.

Problems

  • Code Duplication

  • Maintainability

Solutions

  1. Find repeated patterns (not repeated code).

  2. Create an abstraction.

  3. Parametrize abstraction calls.

  4. Use composition and never inheritance.

  5. 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.

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

More info

Don't repeat

Credits

Photo by Sid Balachandran on Unsplash


Copy and paste is a design error.

David Parnas

Top comments (0)

16 Libraries You Should Know as a React Developer

Being a modern React developer is not about knowing just React itself. To stay competitive, it is highly recommended to explore the whole ecosystem. This article contains some of the most useful React component libraries to speed up your developer workflow.