Using extraction and inheritance to attack duplication

Eliminating code duplication is not always easy, but there are some pretty straightforward refactoring techniques that help in resolving such problems. Once duplicated lines are recognized, the next step is to examine whether the duplicated code could be simply eliminated and replaced with a method call. If this is not viable, then we could resort to the Extract Method refactoring practice.

The Extract Method refactoring pattern

For long parts of duplicated code, we could remove the duplication by moving functionality and code to a single shared place inside our project. All parts of our program could then utilize this shared part of code instead of duplicating it.

From Martin Fowler's Refactoring ...

Get Sonar Code Quality Testing Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.