DRY Principle

When building a large software, you will get overwhelmed by the overall complexity. Managing this complexity is very difficult without proper planning. You as a developer will then decide to conquer this complexity by dividing it in to smaller components. A Component in this system can have its own subsystem, which can be built on top of other small individual components. A component can be a class (if the software is being built in object oriented programming language) which will have a single responsibility. When you divide your system in to components and components into subcomponents, you will have a system where complexity is reduced to the web of individual components, where each component is responsible to handle a single responsibility and can focus on a specific area of a software. Any modification in such kind of a software does not require a change in other logically unrelated components. The DRY principle tells us, to not repeat these responsibilities anywhere else in the system.  
In Their book “The Pragmatic Programmer”, “Andy Hunt” and “Dave Thomas” stated DRY principle as

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

This principle is broadly applicable to not only the code but also, Database Schema, Test Plans, the build system and even documentation.

Suppose that you are building a Book library management system, and it has a component which manages all the operations related to the Users of a system (Let’s call it UserService), like Authenticating the user, Getting the user details, keeping track of user logons etc. according to the DRY principle anything related to the user should not be repeated anywhere in the system but in the UserService or its subsystem. Any changes made to this component does not require changes to other areas of the system. it is very easy maintain such system. Understanding such system becomes very easy as every component in the system is focusing on a specific problem. Developers can alter any part of the system without worrying to much about other area of a system.

Achieving the DRY principle
Duplication can take many forms, duplication of code, duplication of logic, duplication of certain process etc. Many design patterns have the explicit goal of reducing or eliminating the duplication of logic from the system. If an object requires certain things to happen before its creation, we can user the “Abstract factory design pattern”. If an object changes its behavior depending upon the context in which it is executing we can go for “Strategy design pattern”. If there are certain process which is repeated among the objects then we can take help of the “Builder design pattern”. In fact, many design patterns are aimed to resolve the issue of duplication.

Achieving DRY is hard
In real world application achieving 100% DRY principle is hard but not impossible. It requires proper planning, discipline and more importantly it should be followed by every developer in the team, It’s a cultural thing. In Forced deadlines you would see developers writing duplicate and hacky code all over the system, which if not refactored in time, results in a bloatware system. In my opinion the many other principles rely on DRY principle which Aims to Reduce or remove all the duplication from the System. The opposite of DRY is WET which is commonly known as “write everything twice “, “we enjoy typing” or “waste everyone’s time”.  Also make sure you don’t over DRY your system, it is equally bad.

"The difference between stupidity and genius is that genius has its limit."
- Albert Einstein.

If Every developer in the team decides to follow this principle then it is not impossible to achieve a 100% DRY system.

Comments

Post a Comment

Popular posts from this blog

Threading in .Net – Part I – The Basics.

Writing High Quality Functions