Blog for Junior Developers C#/.NET

Thursday, July 04, 2024

Every programmer has different stages in his career. Typically, novice programmers try to write their applications in such a way that they meet all the requirements, regardless of how they write their code. Over time, as subsequent programs are developed, we gain experience, and we also deal with some old, existing code - reflections come. We see that code that has been written before is difficult to maintain. Each subsequent change is more and more difficult to implement. We are wondering how to change it, how to write better, cleaner code, so that if we come back to it after some time, or if our code is taken over by another programmer, he will not have problems understanding it. We realize that we want to write better quality code, there is various information on the Internet on how to improve the quality of our code and what rules to follow. Many people then advise you to familiarize yourself with the term SOLID, but what benefits does it bring and what is SOLID really?

programming-compliant-with-solid-rules-a-guide-for-beginner-programmers.jpg

Programming Compliant with SOLID Rules - A Guide for Beginner Programmers


SOLID is five rules for object-oriented programming. They were proposed by American programmer Robert C. Martin (Uncle Bob) - considered by many as an authority in the world of programming. Uncle Bob is primarily famous for his approach to clean code. The theory of SOLID principles was introduced by Martin in his article in 2000 - Design Principles And Design Patterns. The word SOLID itself means something solid, constructive, or specific, but that's not where the name comes from. They consist of the principles described earlier, which come from each letter in the word SOLID.


S is for Single Responsibility Principle (SRP)


Single Responsibility Principle - Each class should have only one responsibility. There should never be more than one reason to change it in the future.


O is for Open-Closed Principle (OCP)


Open-Closed Principle - A software item should be open to extension but closed to modification. If we have a method that is used in other parts of the system, it should no longer be modified.


L is for Liskov Substitution Principle (LSP)


Liskov Substitution Principle - Functions that use pointers or references to base classes must also be able to use objects of classes that inherit from base classes, without knowing these objects precisely. You can substitute any type of a derived class in place of the base type and it should not lose proper operation.


I like Interface Segregation Principle (ISP)


Interface Segregation Principle - No client should be forced to rely on methods it does not use. Several dedicated interfaces are better than one that is too general.


D is for Dependency Inversion Principle (DIP)


Dependency Inversion Principle - High-level modules should not depend on low-level modules. Both the first and the second should depend on abstractions (interfaces or abstract classes). Abstractions should not depend on details. It is the details that should depend on the abstraction.


Which SOLID principle is the most important?


Recently I received a question from one of the blog readers about which of the SOLID rules is, in my opinion, the most important. There is no such thing, all principles are equally important and, most importantly, these principles are interconnected. So, for example, if we do not follow the principle of liskov substitution, we also most often violate the open-closed principle. If we do not adhere to the principle of interface segregation, we are also likely to violate the principle of single responsibility.

solid-castle.png

SUMMARY


Most programmers know how to decipher the SOLID acronym, but not everyone follows these rules. SOLID principles are especially difficult for beginner programmers to understand, but that's no wonder, because understanding them takes time and practice. It is worth knowing what they are, primarily because by following them, our code will be of better quality, it will be easier to develop and maintain, and our systems will be testable. Additionally, recruiters like to ask about SOLID during job interviews for junior programmer positions. If you are a beginner programmer and you do not yet fully understand the principles presented by Martin - write in a comment, I will be happy to expand my articles. You also need to remember that it takes some practice to understand all the rules. In the following articles, I will try to familiarize you with unit testing in C#. When writing good tests, it is necessary to follow the SOLID principles.

Author of the article:
Kazimierz Szpin

KAZIMIERZ SZPIN
Software Developer C#/.NET, Freelancer. Specializes in ASP.NET Core, ASP.NET MVC, ASP.NET Web API, Blazor, WPF and Windows Forms.
Author of the blog CodeWithKazik.com

Previous article - SOLID - DIP - Everything You Should Know About the Dependency Inversion Principle
Next article - Automation Tests Explained in One Article
Comments (1)
Kazimierz Szpin
KAZIMIERZ SZPIN, czwartek, 11 lipca 2024 11:08
What do you think about this article? I'm curious about your opinion.
Dodaj komentarz

Search engine

© Copyright 2024 CodeWithKazik.com. All rights reserved. Privacy policy.
Design by Code With Kazik