O'Reilly logo

Sonar Code Quality Testing Essentials by Charalampos Arapidis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required