Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 17, 2022 01:29 am GMT

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"

Why clean code is important in software development.

Code is clean if it is easily maintainable and understandable by everyone and has fast execution when compiled. Clean code improves code quality and gives confidence in software development. Clean code also cost less in hosting an application on a cloud platform.

Now let's get to the principles that have been embraced by the community, independent of the language you are working in. 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 to >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 follows 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 duplication the above code will be if the student were asked to reverse five words.

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

The student can just 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.

Hence the student can improve the code to be the code below :

// reverse word functionconst reverseWord = (word) => {    const reverseWord = word.split('').reverse().join('');    return reverseWord;}console.log(reverseWord("begins")) // prints snigebconsole.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 functionconst 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 above code is simple, small, and easy to understand which follows the KISS principle.

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

YAGNI

YAGNI fully means (You Aren't Gonna Need It). 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


Original Link: https://dev.to/desmondowusudev/best-practicesdry-kiss-and-yagni-2a60

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To