DEV Community

Cover image for Software Development Best Practices(DRY, KISS and YAGNI)
Desmond Owusu Ansah
Desmond Owusu Ansah

Posted on • Updated on

Software Development Best Practices(DRY, KISS and YAGNI)

What is DRY, KISS, YAGNI?

They are just acronyms of common best practices and best principles to write clean code. In this article, we will discuss what they mean and why they are important. Let's first discuss "why clear code is important"

Importance of clean code in software development.

Would be great to know why it's important to make code easier to maintain and understandable. Would be great to cover some of the background of it. E.g. code is much more often read compared to written. It helps when working in teams. It helps other engineers to get quicker in context. Clean code improves code quality and gives confidence in software development.

Now let's get to the principles that have been embraced by the community, independent of the language you are working on. Some of the most popular are:

DRY

DRY simply means (Don't repeat yourself!). This principle clearly means we should try to avoid having duplicated code. Instead, we should reuse your code when possible.

To make this clear let us discuss this scenario;

A student was asked to write a JavaScript program to reverse two words begins and resume.

Student code

// reverse "begins"
const reverseBegins = () => {
   const reverseWord = "begins".split('').reverse().join('');
   return reverseWord;
}
console.log(reverseBegins()) // prints snigeb

// reverse "good"
const reverseGood = () => {
   const reverseWord = "good".split('').reverse().join('');
   return reverseWord;
}
console.log(reverseGood()) // prints doog
  • Did the Student follow DRY?

No

  • Why the student didn't follow DRY

Explanation: This student code works perfectly as expected but it duplicates a block of code that performs the same function hence the code above does not obeys the DRY principle. Assume how complicated and duplicated the above code will be if the student were asked to reverse five(5) words.πŸ˜‚

  • How can this student improve the code to make it DRY?

The student can use a function with a parameter. So whenever the student wants to reverse a word, the student can pass the word as a parameter when the function is called. For instance, In the code below, a function called reverseWord takes in word as a parameter which reverses any word that will be passed as a parameter when the function is called. So it now will be simple when I have to reverse five(5) words as compared to the student code.

// reverse word function
const reverseWord = (word) => {
    const reverseWord = word.split('').reverse().join('');
    return reverseWord;
}
console.log(reverseWord("begins")) // prints snigeb
console.log(reverseWord("good")) // prints doog

KISS

KISS means (Keep It Simple, Stupid). KISS simply means we should try to avoid unnecessary complexity, we shouldn't over-engineer our code and there should be no further explanations. Our code should be simple, small, and easy to understand.

Let's see if the JavaScript code below which gets and returns items from the localStrorage of the browser but returns an empty array if there are no items in the localStrorage.

// get items from localStorage function
const getItemsFromStore = () => {
    const items = JSON.parse(localStorage.getItem("Items"))
    if (!items) {
        return [];
    } else {
        return items;
    }
}

I understand, if someone says nothing is wrong with this code but let's realize that the use of if-function in this code makes it complicated and long.

The code below is simple, small, and easy to understand which follows the KISS principle because it far away from been complicated.
In the code below the items variable is representing two things JSON.parse(localStorage.getItem("Items")) and an empty array []. The items variable only returns an empty array [] when JSON.parse(localStorage.getItem("Items")) is falsy which is exactly the same as the using the if-statement to check if it’s falsy.

// get items from localStorage function
const getItemsFromStore = () => {
    const items = JSON.parse(localStorage.getItem("Items")) || [];
    return items;
}

YAGNI

YAGNI fully means (You Aren't Gonna Need It).The YAGNI principle says you shouldn't add anything you don't strictly need. Functionality should only be implemented in a program when it is clear that it is really needed. Try to avoid the temptation of adding the most trendy technologies just because you think they may be useful in the future. Add things gradually, when they are really needed.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on LinkedIn or Facebook

Top comments (0)