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

Duplication Vs. Redundancy in Code

DZone's Guide to

Duplication Vs. Redundancy in Code

''Redundancy'' may be a better word choice than ''duplication'' when talking about code quality. Why does it matter?

· DevOps Zone ·
Free Resource

Discover how you can reduce your Kubernetes installation from 22 steps to 1.

You may have noticed that I don’t use the XP term duplication when talking about code quality. I prefer to use the term redundancy instead. This is because duplication is the most obvious form of redundancy but redundancy can take many other forms that are far more subtle and harder to detect.

When I ask developers if non-identical code can be redundant, they say yes. Think of a “for” loop and a “for each” loop iterating through the same collection.

When I ask developers if identical code can be non-redundant, they have a harder time answering me. However, after some thought, they realize that it’s also true. For example, a line of code that reads “index++;” can be used to iterate through a list of names in one part of the code and a list of numbers in another part of the code.

Redundancy is not a repetition of form, but rather a repetition of intent. We are trying to do the same thing in multiple places and that makes it redundant.

Sometimes, I can’t see the redundancy in my code, but I recognize when I write a redundant test that my code must also be redundant.

I say that 95 percent of all redundancy is easy to spot in code, but that last five percent is worth uncovering even if it takes an extra effort because I find that that last little bit of redundancy often hides the essence of a good design and when I remove it, I suddenly see patterns that I hadn’t noticed before.

In his deceptively simple and excellent book, Test-Driven Development By Example, Kent Beck says that if you pay attention to removing duplication in your code and in your test, you’ll end up with good quality code and good quality tests. He is right, but the same could be said about any of the other good code qualities such as cohesion or encapsulation. When you improve one code quality, the others tend to also improve. That’s because they’re facets of the same gem, which is good code. When you improve one aspect of code and you see other aspects of code also improve, then you know you’re on the right track.

Download the Kubernetes cheatsheet to learn more about easy it is to run Kubernetes on any infrastructure with Mesosphere DC/OS

Topics:
devops ,code quality ,software development

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}