Code Quality Basics - What Is Code Duplication?


Having minimum code duplication is one of the Four Principles of Simple Design. In this article, we look at why we should worry about code duplication, and what are the best practices to follow to reduce it.

You will learn

  • What Is Code Duplication?
  • Why Is Code Duplication Bad?
  • How do you measure Code Duplication?
  • How can ensure that Code Duplication standards are Adhered to?

Article Series

This is fourth article in a series of 6 articles on important code quality terminology:

What Is Code Duplication?

Same block of code repeating multiple times is the most basic form of Code Duplication.

Why Is Code Duplication Bad?

Suppose there is a change needed in one of the places where a code block is used. When the change is made, it needs to be replicated at all other places where it occurs. If a code block of 40 lines occurs at 10 places in your application, there is a chance that a developer modifies it only in 8 places out of the 10. The code is now potentially broken!

When you have code duplication, maintaining the code becomes very difficult.

How To Measure Code Duplication?

There are static analysis tools such as SonarQube that measure code duplication as part of estimating technical debt:

image info

image info

As you can see, code duplication is mentioned under “Duplication” at the bottom. It gives an idea of the percentage of duplicate code in the project.

Typically, you have some extent of duplication in your code. A general measure of controlled duplication is a limit of 5%. A project having less than 5% of code duplication is considered very good.

It is important to continously evaluate code duplication and identify improvements.

Looking At Code With Duplication

Have a look at the following source file, from the project which we ran SonarQube on, previously:

< ParenthesizedTreeImpl.java >

image info

You can see that the duplicated block is highlighted by a bold-grey vertical line. We can click on it to see the duplicated blocks:

image info

SonarQube indicates that SynchronizedStatementTreeImpl.java, SwitchStatementTreeImpl.java and ParenthesizedTreeImpl.java have the same code block duplicated across them.

Avoiding Code Duplication

Make Use Of Inheritance

The simplest solution for the problem at hand is to define a super class, and define the duplicated blocks as code within its methods.

Define A Utility Class Or Method

This is the alternative available when reuse by inheritance is not the option that works.

The catch is that there is no single solution! It depends on the duplicate code block, and the context in which it is being used.

Do check out our video on the same topic:

image info

Summary

In this article, we had a close look at code duplication. We explored why code duplication is not good, and what are the ways in which it can be detected and measured. We also had a brief look at how to avoid it.

10 Step Reference Courses

Image Image Image Image Image

in28Minutes is helping 300,000 Learners across the world reach their learning goals. Click here for the complete catalogue of 30 Courses.

Related Posts

Spring Boot Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. This resources will help you learn and gain expertise at Spring Boot.

Spring and Spring Boot Video Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. Here's a list of video tutorials and courses for you

Software Design - Separation Of Concerns - with examples

Software architects and programmers love having Seperation of Concerns. What is it? Why is it important? Let's get started.

Object Oriented Software Design - Solid Principles - with examples

Software design is typically complex. Object oriented design takes it to the next level. There are a number of design patterns and other stuff to be aware of. Can we make things simple? What are the goals to aim for when you are doing object oriented design? SOLID Principles is a great starting point for Object Oriented Design.

Software Design - Open Closed Principle - with examples

Open Closed Principle is one of the SOLID Principles. You want your code to be easily extended. How do you achieve it with minimum fuss? Let's get started.

Software Design - What is Dependency Inversion Principle?

Dependency Inversion Principle is one of the important SOLID Principles. Dependency Inversion Principle is implemented by one of the most popular Java frameworks - Spring. What is it all about? How does it help you design good applications?

Introduction to Four Principles Of Simple Design

With agile and extreme programming, the focus is on keeping your design simple. How do you keep your design simple? How do you decide whether your code is good enough?

Software Design - Single Responsibility Principle - with examples

For me, Single Responsibility Principle is the most important design principle. What is Single Responsibility Principle? How do you use it? How does it help with making your software better? Let's get started.

REST API Best Practices - With Design Examples from Java and Spring Web Services

Designing Great REST API is important to have great microservices. How do you design your REST API? What are the best practices?

Designing REST API - What is Code First Approach?

Designing Great REST API is important to have great microservices. Code First approach focuses on generating the contract from code. Is it the best possible approach?