Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Code Smell 46 — Repeated Code

Maximiliano Contieri
Dev Genius
Published in
2 min readDec 8, 2020

Photo by Sid Balachandran on Unsplash

Problems

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.

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

More info

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Maximiliano Contieri

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com

Responses (1)

Write a response

https://codeclimate.com/ is a great tool for finding duplication.

8