question archive Explain the DRY principle of software development

Explain the DRY principle of software development

Subject:Computer SciencePrice:2.89 Bought3

Explain the DRY principle of software development.

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

DRY stand for "Don\'t Repeat Yourself," a basic principle of software development aimed at reducing repetition of information. The DRY principle is stated as, "Every piece of knowledge or logic must have a single, unambiguous representation within a system."

Violations of DRY
"We enjoy typing" (or, "Wasting everyone\'s time."): "We enjoy typing," means writing the same code or logic again and again. It will be difficult to manage the code and if the logic changes, then we have to make changes in all the places where we have written the code, thereby wasting everyone\'s time.

How to Achieve DRY
To avoid violating the DRY principle, divide your system into pieces. Divide your code and logic into smaller reusable units and use that code by calling it where you want. Don\'t write lengthy methods, but divide logic and try to use the existing piece in your method.

DRY Benefits
Less code is good: It saves time and effort, is easy to maintain, and also reduces the chances of bugs.

One good example of the DRY principle is the helper class in enterprise libraries, in which every piece of code is unique in the libraries and helper classes.

 

For example, if you\'re building a content management system, the part that is responsible for user management will be a component. This component can be divided into further subcomponents, like role management, and it may communicate with other components, such as the security component.

As you divide systems into components, and, further, components into subcomponents, you will arrive at a level, where the complexity is reduced to a single responsibility. These responsibilities can be implemented in a class (we assume that we\'re building an object-oriented application). Classes
contain methods and properties. Methods implement algorithms. Algorithms and - depending on how obsessive we want to get - subparts of
algorithms are calculating or containing the smallest pieces that build your business logic.

 

There are many ways of achieving DRYness. Hunt and Thomas suggested (among other things) code generators and data transforming. But, essentially, DRY is a philosophy that packages logic into representations.

As every part of your application can be seen as representation, every part exposes specific fragments of your underlying logic: The user management exposes access to registered users of the CMS, the user class represents a single user and exposes his properties (like the username). It retrieves the properties, via the representation of the database.

DRY and modular architecture require good planning. To achieve a representational hierachy from bottom-up, divide your application in a hierarchy of logically separated smaller parts and let them communicate with each other. If you have to manage larger projects, organizing them into components and using DRY within the components is a good idea. Try to apply the following rules:

Make a visual hierarchy of your software application and map the main components to it. Complex projects may require a dedicated map for each component.
If you\\\'re arriving at a level of connected responsibilities, you may want to switch to UML diagrams (or similar).
Before writing a chunk of code, name its hierarchy in your software project. Define what it\\\'s representing, and be sure you know its role in the surrounding component.
Define what the representation should expose to other parties (like functions to execute SQL in a database driver) and what it should hide (like the database credentials).
Ensure that representations do not rely on representations of another complexity level (like a component that relies on a class in another component).

Related Questions