Software Development Best Practices(DRY, KISS and YAGNI)
Tags: javascript, discuss, programming, productivity
Published: 2023-03-18 13:31:28

## **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 `begin`and `resume`

Student code

![Coding image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2or7f1pgao9yxd4s5m9.png)

- 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.


![Coding Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2thb3q196h7bnd7lzu9d.png)
**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`.

![Coding Images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wkijh0g9g1dhwjregsdg.png)
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.

![Coding Images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bl80xkd386jtbnh9rzo8.png)
**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/joseph-david-01a8a5243?lipi=urn%3Ali%3Apage%3Ad_flagship3_profile_view_base_contact_details%3BN7OhQ6WFSy%2BtDYP1iGM9lQ%3D%3D) and [Twitter](https://twitter.com/jodavid444dave)