Pages

Friday, November 08, 2013

The Evils of Duplication

Duplication is generally considered a bad practice. It should be avoided in production code as much as possible. Programmers are the people responsible for collecting, maintaining and organizing knowledge. Knowledge, as we all know, is never constant. It changes from time to time. Our knowledge about the requirements may change while communicating with the customer. The technologies and tools change rapidly with time. These changes create instabilities which always make programmers remain in the maintenance mode.

Most of the people do have a misconception that maintenance starts only when the product is released. That’s not at all right! With the arrival of new requirements and changes in the environment, maintenance needs to be a regularly followed activity.

Duplication is easy to create but ends up creating a maintenance nightmare. The only way to develop maintainable and reliable code is to apply the DRY (Don’t Repeat Yourself) principle.

DRY, as it sounds, is not so dry!

Imagine that we have the same information represented at two or more places. If we change data at one place, we should remember to change it in other places as well. Otherwise, two of these may contradict each other, resulting in serious problems.

“It isn't a question of whether you'll remember: it's a question of when you'll forget.”

That’s where the DRY principle comes in.

How Does Duplication Arise?

Duplication can be classified mainly into four categories:

Imposed Duplication: This happens when the developers are left with no other choice than to impose duplication. There are situations where duplication is unavoidable or the environment seems to require duplication. But with a bit of ingenuity you can normally remove the need for duplication.

Inadvertent Duplication: In this scenario, the developers don’t realise that they are duplicating information, and generally it happens by accident.

Impatient Duplication: This is the most easily avoidable duplication. There might be situations where duplication makes things easy. In such cases, developers go lazy and accept duplication. There could be many reasons for that. In times like these, developers shouldn’t forget the fact that:

"Shortcuts make for long delays."

Investing time now saves time later.

Interdependent duplication: This is the hardest type of duplication. Here, different people in a team (or different teams) duplicate the same piece of information. This happens often due to lack of communication (obviously, something that stands against agile practices!). This can be also avoided with a clean design, a strong project leader and a well understood division of responsibilities.

Whatever you develop, make it easy to reuse. Failure to develop reusable code leads to duplication.

summary of The Evils of Duplication, from The Pragmatic Programmer: From Journeyman to Master

No comments:

Post a Comment