Duplicated Code

When identical or very similar code fragments appear in multiple places.

Sign of Smell

Duplication can occur when multiple developers work on different sections of the same codebase, or when a newbie fails to check the existing code while creating their own one. This happens because they are focused on their current tasks too much and may not know that their colleagues have already written similar code that could be reused for their purpose.

As a result, the codebase becomes fragmented, and unnecessary repetition is introduced, leading to decreased maintainability and increased chances of bugs and errors. It is crucial for developers to communicate and collaborate effectively to minimize duplication and maximize code reuse, ensuring a more efficient and streamlined development process.

There’s also more subtle duplication when specific parts of code look different but actually perform the same job. This kind of duplication can be hard to find and fix.

Sometimes duplication is purposeful. When rushing to meet deadlines and the existing code is “almost right” for the job, novice programmers may not be able to resist the temptation of copying and pasting the relevant code. And in some cases, the programmer is simply too lazy to de-clutter.

Reason of Smell

  • Readability: Code duplication makes the codebase harder to read and understand. Developers who work on the codebase may struggle to grasp the intent and logic behind duplicated sections, as they have to navigate between multiple locations to piece together the entire functionality.

  • Violation of the DRY principle: DRY (Don't Repeat Yourself) encourages developers to write code in a modular and reusable manner. This means identifying common functionality and encapsulating it in functions, classes, or modules. By doing so, this functionality can be accessed from multiple parts of the codebase without duplication.

  • Code Bloat: Duplicated code can lead to code bloat, where the codebase becomes unnecessarily large and complex. This can affect the performance of the software and make it harder to maintain and extend.

  • Error-Prone: Duplicated code increases the chances of introducing errors into your software. If a bug is found in one instance of duplicated code and fixed, it's easy to forget to make the same fix in other duplicated instances. This can lead to inconsistent behavior and hard-to-trace bugs.

  • Maintenance: When the same code is duplicated in multiple places within a codebase, it becomes more difficult to maintain and update the software. Any changes or bug fixes that need to be applied to that code must be made in multiple locations, increasing the likelihood of introducing inconsistencies or errors.

Refactoring Recipes

Similar to the Dead code smell, duplicate code can also refer to anything in our codebase - a variable, parameter, field, method, or even a class. However, in most cases, it is likely to be methods or functions. Hence, the skills needed for refactoring also differ based on the type of duplicate code.

Here are some refactoring skills that can match Duplicated Code smell from Smells to Refactorings Cheatsheet.

  • Chain Constructors

  • Extract Composite

  • Extract Method

  • Extract Class

  • Form Template Method

  • Introduce Null Object

  • Introduce Polymorphic Creation with Factory Method

  • Pull Up Method

  • Pull Up Field

  • Replace One/Many Distinctions with Composite

  • Substitute Algorithm

  • Unify Interfaces with Adapter

Reference

https://refactoring.guru/smells/duplicate-code

Last updated