In our daily programming, we see three types of software design principles i.e DRY, KISS, and YAGNI.
- DRY PRINCIPLE – Don’t repeat yourself
- KISS – Keep it simple stupid
- YAGNI – You aren’t gonna need it
- Curly’s Law – Do One Thing
DRY PRINCIPLE :
The aim of this principle is to reduce the repetition of information. This principle is stated as “Every piece of knowledge must have a single, unambiguous, representation within a system “.
To accomplish this, divide your system into pieces. Don’t write long methods; instead, divide logic into smaller reusable pieces as much as possible and try to reuse existing pieces in your method.
KISS – KEEP IT SIMPLE STUPID :
Each method should only solve one small problem, rather than many. If your method contains a large number of conditions, divide it into smaller methods. It will not only be easier to read and maintain, but it will also allow you to find bugs much faster.
Here below, Two different methods doing the same implementation,
public String weekday1(int day) {
switch (day) {
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
case 7:
return "Sunday";
default:
throw new InvalidOperationException("day must be in range 1 to 7");
}
}
public String weekday2(int day) {
if ((day < 1) || (day > 7)) throw new InvalidOperationException("day must be in range 1 to 7");
string[] days = {
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
};
return days[day - 1];
}
YAGNI – You aren’t gonna need it
Don’t add any functionality until it’s absolutely necessary; in other words, only write the code you need right now. If you don’t think you’ll need it, leave it out. Don’t think about what might be needed in the future as you write your code logic.
There are two primary reasons to use YAGNI:
You save time because you avoid writing code that you later discover is unnecessary.
Your code is better because you don’t pollute it with ‘guesses’ that turn out to be mostly incorrect but persist.
Curly’s Law – Do One Thing
A class, function, or variable should only mean one thing. It should not mean one thing in one context and mean something completely different in another. It should not mean two things at the same time. It should only mean one thing and mean it all the time.
People having good knowledge of Financial accounting can get an accounting certification from StudySection to increase their chances of getting a job in this field. You can get a foundation level certification if you are new to Financial Accounting or you can go for advanced level certification if you have expert level skills in Financial accounting.