Code Smell 46 — Repeated Code

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

Photo by Sid Balachandran on Unsplash

Problems

Solutions

  1. Create an abstraction.
  2. Parametrize abstraction calls.
  3. Use composition and never inheritance.
  4. Unit test new abstraction.

Sample Code

Wrong

Right

Detection

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

Conclusion

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

Dev Genius

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

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Dev Genius

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

Maximiliano Contieri

Written by

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

Dev Genius

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

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store