The principle and practices of software development have always interested me. In this blog, we are going to talk about the first principle of coding that I ever learned about i.e. Don't Repeat Yourself principle.
The DRY principle is stated as “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system” by Andy Hunt and Dave Thomas in their book The Pragmatic Programmer. In DRY code, we do functioning to codes that are repetitive and perform the same task multiple times in your code. And there should only ever be one copy of any important piece of information.
Let's say you are programming an application that is a blog reading site and there are 100 articles on the site. If you program 100 different view pages for the article, then it will become very high maintenance and expensive process. So, in this case, you need to write a base template in your code and serve the same base template for every article view.
Starting your DRY code:
- Know where your codes are repeating.
- Change your repetition into the functional module.
- Always remember that
less code = low maintainence
- If your codes are written in object-oriented style then always classify properties in
P.S. We also have the WET principle:
In contrary to the DRY principle, the WET principle means
Write Everything Twice. These two principles seem to be inseperable from each other. The WET principle has widely variant interpretations across the internet. It's not clearly stated that when was this principle formulated. But some articles like this have really nice interpretations of the WET principle.