DEV Community

Cover image for SOLID: Why You Should Learn It
Pedro Masson
Pedro Masson

Posted on

SOLID: Why You Should Learn It

If you're new to the tech world, maybe you haven't heard about the SOLID principles — yet. As we evolve in our career, we definitely will see many articles, videos, and books about SOLID. But is it that important?

The answer is, it depends. Depends on if you want to write a good code, or a bad one. It's as simple as that. If you want to learn principles that will help you to write good code, stay in this article, if not, feel free to leave.


What does it mean?

SOLID is an acronym that stands for Single responsibility, Open closed, Liskov substitution, Interface segregation and Dependency inversion.
SOLID Acronym


Why is it important?

Even in small projects, the absence of these principles can be dangerous to the codebase. It will eventually grow with structural problems and a lot of coupling. Let's see a practical example:

class Employee
{
   public string Name { get; private set; }
   public string Seniority { get; private set; }
}

class SeniorityPayment
{
   private decimal _salary;

   public decimal CalculateSalaryBasedOnSeniority(Employee employee)
   {
      if (employee.Seniority == "Intern")
      {
         //do something
      }
      if (employee.Seniority == "Junior")
      {
         //do something
      }
      if (employee.Seniority == "Mid Level")
      {
         //do something
      }
      if (employee.Seniority == "Senior")
      {
         //do something
      }
   }
}
Enter fullscreen mode Exit fullscreen mode

You guys already understand what's the issue with this code, no? We already have many if statements, and if the company decides to add some seniority above the senior, another if will be added to the code. How can we solve this?
Using SOLID, the Open/Closed principle.

interface EmployeeSenioritySalary
{
   public decimal CalculateSalary();
}

class InternEmployee : EmployeeSenioritySalary
{
   public decimal CalculateSalary()
   {
      // now we can calculate the intern salary separately
   }
}

class SeniorityPayment
{
   private decimal _salary;

   public decimal CalculateSalary(EmployeeSenioritySalary employee)
   {
      _salary = employee.CalculateSalary();
      // implements the logic
   }
}
Enter fullscreen mode Exit fullscreen mode

Now, the class that wants to calculate the salary does not know if the employee is an intern, a junior, or a senior, it only does its job. Why this code is an example of using the Open/Closed principle? Because the class now is open for extension, but closed for modification.

It looks much better now, don't you agree? Well, if you invest some time in learning this and the others SOLID principles, I'm pretty sure the overall quality of your code is going to skyrocket :D

If you have any questions or suggestions, leave them below on the comments section, or reach out to me on linkedin.

Top comments (2)

Collapse
 
tadeubdev profile image
Tadeu Barbosa

Great post! I agree! If you wanna be a good developer then you have to start studying SOLID.

Collapse
 
jangelodev profile image
João Angelo

Hi Pedro Masson,
Thanks for sharing