Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Duplication, You're Welcome

DZone's Guide to

Duplication, You're Welcome

Check out the DRY principle of learning and what the DRY principle actually means in regards to duplicated lines of code.

· Agile Zone ·
Free Resource

Discover how you can take agile development to the next level with low-code.

There are models to describe the process of learning, such as the Dreyfus model or Shuhari, but I'd like to explain another process when learning a new technical skill. It is composed of 2 phases:

  1. Fascination. This is when we learn a new concept that sounds really good to solve our problems, and we apply it in any place we have an opportunity. We are so convinced about its advantages that it seems we have a radar to detect a place in which we can apply it. It's like the first stage at Shuhari, but we are absolutely fascinated and we don't believe in another world.
  2. Questioning. This is when we have assimilated that concept and we are able to decide where it's convenient to implement it. We have learned from the previous stage and now we are moderate and reasonable about our decisions.

But sometimes it's really difficult to jump to the second stage because we are continuously receiving messages about a concept for years. It's the case of no duplication.

This week, I was reviewing a test class with 4 extracted parameterized methods (for 4 different types of expectations) and 6 extracted methods (for 6 different JSON files). I sat with the author of that class and we applied inline method refactoring. After that, we realized that the test class was more readable and understandable without those 10 additional methods, despite having duplicated source code.

A few weeks ago, I helped to separate two different modules. They were together, because they had some similar domain objects, although they didn't use the same properties. It was really difficult to know which properties and methods needed each module.

What's the reason of those extracted methods or that reuse?

Alerts: duplicated lines (%) > 10%

One of the metrics we can find in static code analysis tools is the percentage of duplicated lines. Managers need metrics and this one is usually included as a guarantee of quality code. However, striving for a certain number of duplicated lines in a board can cause these consequences:

  • Over-generalization
  • Over-anticipated abstractions
  • Early decisions about common parts
  • Coupling
  • Levels of indirection, adding difficulty to understand the source code

We tend to associate this metric with D.R.Y. Principle (Don't Repeat Yourself), but let's read the principle carefully:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

This principle is talking about knowledge, not duplicated lines. Two pieces of code could be apparently similar, but have completely different reasons to change, because they represent different pieces of knowledge. Therefore, removing that duplication can muddle the logic and make any future change worse.

That's the reason why I usually highlight knowledge when explaining the third rule of simple design:

No knowledge duplication.

To sum up, some recommendations:

  • Try to postpone your decisions about duplicated code, because it's a question of trade-offs
  • When you find two apparently similar pieces of source code, question whether you find a reason to have to change them at the same time
  • Use the metric about duplicated lines only as guidance, but don't try to strive for a number in a board, compromising the understanding of source code

Further reading

Download this eBook to learn how to prepare your business for agile adoption, how to ensure the proper business-IT collaboration that is critical for agile development, and how to choose the right stakeholders to increase productivity and enable accelerated time-to-value.

Topics:
quality ,duplication ,metrics ,agile ,code duplication ,dry principle ,learning methods

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}