Software Development Best Practices(DRY, KISS and YAGNI)
Tags: javascript, discuss, programming
Published: 2022-07-17 01:29:53


## 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_ 

> ```javascript
>// 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._

>```javascript
// 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`.

>```javascript
// 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._ 

>```javascript
// 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](https://www.linkedin.com/in/desmond-owusu-ansah-09274a223/) or [Facebook](https://web.facebook.com/desmondOwusuDev)