Software Design Principles DRY, KISS, YAGNI

In this article, I am going to explore software design principles and their benefits and why design principles are useful for us and how to implement them in our daily programming. We will see DRY, KISS, and YAGNI software design principles.

We will discuss -

  • What software design principles are.
  • What DRY, KISS, and YAGNI are.
  • Why we need them.
  • Their benefits.
  • How to implement them in our daily programming.

DRY PRINCIPLE - DON’T REPEAT YOURSELF

We will see,

  • What is DRY.
  • Why we need it.
  • What are its benefits.
  • How to implement it in our daily programming.

DRY stands for DON’T REPEAT YOURSELF and is a basic principle of software development, aimed to reducing repetition of information. The DRY principle is stated as "Every piece of knowledge must have a single, unambiguous, representation within a system “. Reference wiki here: DRYwiki

Violations of DRY: "we enjoy typing" or "waste everyone's time “.

How to Achieve

To achieve, divide your system into pieces. Don’t write lengthy methods, divide logic into smaller reusable pieces as much as possible and try to use the existing piece in your method.

Good example for DRY principle,

Enterprise libraries, helper class, every piece of code is unique in the libraries and helper classes.

KISS - KEEP IT SIMPLE STUPID

A big guy said: The programming language is for humans to understand, computers can only understand 0 1, so keep coding simple and straight, which should be easily understandable by human beings.

Keep your methods small, each method should never be more than 40-50 lines.

Each method should only solve one small problem, not many use cases. If you have a lot of conditions in the method, break these out into smaller methods. It will not only be easier to read and maintain but also can find bugs a lot faster.

  • Right-click and refactor in the editor.

There are two codes shown below, both methods are doing the same thing,

  1. public String weekday1(int day) {  
  2.     switch (day) {  
  3.         case 1:  
  4.             return "Monday";  
  5.         case 2:  
  6.             return "Tuesday";  
  7.         case 3:  
  8.             return "Wednesday";  
  9.         case 4:  
  10.             return "Thursday";  
  11.         case 5:  
  12.             return "Friday";  
  13.         case 6:  
  14.             return "Saturday";  
  15.         case 7:  
  16.             return "Sunday";  
  17.         default:  
  18.             throw new InvalidOperationException("day must be in range 1 to 7");  
  19.     }  
  20. }  
  21. public String weekday2(int day) {  
  22.     if ((day < 1) || (day > 7)) throw new InvalidOperationException("day must be in range 1 to 7");  
  23.     string[] days = {  
  24.         "Monday",  
  25.         "Tuesday",  
  26.         "Wednesday",  
  27.         "Thursday",  
  28.         "Friday",  
  29.         "Saturday",  
  30.         "Sunday"  
  31.     };  
  32.     return days[day - 1];  
  33. }  

Now, you have to decide which one is clearer.

YAGNI - You aren't gonna need it

You are not going to need it, or YAGNI is a principle of extreme programming.  XP. XP is generally used in the Agile software development process.

YAGNI says- do not add any functionality until it's deemed necessary; in other words,  write the code which you need in the current situation. Don't add anything if you think will need it. Add your code logic for the present, don't think of what may be needed in the future. 

Point of Interest

While writing any code or module keep software design principles in mind and use them wisely, make them your habit so you don't need to keep remembering every time, it will save development time and make your software module robust which could be easy to maintain and extend.