DEV Community

Cover image for Understanding SOLID, DRY, YAGNI and KISS
Gabriel Ferreira Duarte
Gabriel Ferreira Duarte

Posted on

Understanding SOLID, DRY, YAGNI and KISS

Hi! I brought an extremely important subject to the software development process, quality✨

Let's start from the basics, looking at some of the most important and essential principles for every developer.

The SOLID, DRY, YAGNI and KISS programming principles are fundamental for the development of quality software that is easy to maintain. They provide guidelines for writing clean, organized, and maintainable code.


Starting with the simplest one, the DRY principle (Don’t Repeat Yourself) is one of the fundamental rules of programming that is based on the idea that the code should be as simple and direct as possible, without the need for unnecessary repetitions.

By applying the DRY principle in software development, it is possible to ensure that the code is easier to understand, maintain and evolve. This is accomplished through techniques such as code reuse, creating generic functions, and organizing code into modules and classes.

def soma(a, b):
    return a + b

resultado1 = soma(5, 3)
resultado2 = soma(10, 2)
Enter fullscreen mode Exit fullscreen mode

In addition, the application of this principle also helps to avoid errors and bugs in the code, as it avoids the creation of duplicate code that can be a source of problems.


The YAGNI (You Ain’t Gonna Need It) principle is a guideline to avoid adding unnecessary features. He encourages the team to only focus on functionality that is absolutely necessary for the current project and to avoid adding features that may be useful in the future but are not essential now.

For example, in the above code we don't need to implement a function for subtraction since we don't need it now.


About KISS (Keep It Simple, Stupid) the idea is to keep the code simple and easy to understand, simple as that 😎

He encourages the team to avoid unnecessary complexity and focus on simple, easy-to-understand solutions.

const frutas = ["Maçã", "Banana", "Manga"];
console.log(frutas);
Enter fullscreen mode Exit fullscreen mode

In the example above we have a list of fruits and then we display this list on the console. What if now we do it the following way?

const frutas = ["Maçã", "Banana", "Manga"];
let totalFrutas = frutas.length;
let listaFrutas = "";
for (let i = 0; i < totalFrutas; i++) {
    listaFrutas += frutas[i] + ", ";
}
console.log("Lista de frutas: " + listaFrutas);
Enter fullscreen mode Exit fullscreen mode

We have the same result, but in a much more complex way we can display this list.


And finally the most complex one, the SOLID principle.

It is composed of five other interrelated principles: Single Responsibility Principle (SRP), Open-Closed Principle (OCP), Liskov Substitution Principle (LSP), Interface Segregation Principle (ISP) and Dependency Inversion Principle (DIP).

  • The SRP states that each class should have a single responsibility, which means it should have only one reason to change.

  • OCP states that classes should be open for extension but closed for modification.

  • LSP states that subclasses must be replaceable by the superclass.

  • The ISP states that the interfaces must be specific to their customers' needs.

  • Finally, DIP states that high-level modules should not depend on low-level modules, but both should rely on abstractions.

We will analyze this principle better in the future...


And what do we learn from all of this?

In short, that the programming principles seen (as well as the many other existing ones) are fundamental for the development of quality software.

They provide guidelines for writing clean, organized, and maintainable code, avoiding adding unnecessary features, and focusing on simple, easy-to-understand solutions.

By adopting these principles, teams can develop robust, scalable software that will be easy to maintain and evolve over time.

I hope you enjoyed it and that this content is very useful! See you next time! 🚀

Top comments (1)

Collapse
 
bendevoficial profile image
Bendev Junior

Nice!