Jump to content

SOLID: Difference between revisions

From Knowledge Base
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 26: Line 26:
SRP not only makes code cleaner but also aligns with the values of clarity, focus, and accountability.
SRP not only makes code cleaner but also aligns with the values of clarity, focus, and accountability.


[[File:Howmanysports.gif|frameless|center]]
== Open/Closed Principle (OCP) ==
 
= Open/Closed Principle (OCP) =
The Open/Closed Principle is a key tenet of object-oriented design that stipulates that software entities (classes, modules, functions, etc.) should be open for extension but closed for modification. This means that the behavior of a module can be extended without modifying its source code, which helps prevent issues when the system evolves.
The Open/Closed Principle is a key tenet of object-oriented design that stipulates that software entities (classes, modules, functions, etc.) should be open for extension but closed for modification. This means that the behavior of a module can be extended without modifying its source code, which helps prevent issues when the system evolves.


= Liskov Substitution Principle (LSP) =
== Liskov Substitution Principle (LSP) ==
The Liskov Substitution Principle asserts that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program. In essence, subclasses should not alter the expected behavior of the base class.
The Liskov Substitution Principle asserts that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program. In essence, subclasses should not alter the expected behavior of the base class.


= Interface Segregation Principle (ISP) =
== Interface Segregation Principle (ISP) ==
The Interface Segregation Principle (ISP) advocates for designing interfaces that are specific to client needs. It suggests that no client should be forced to depend on methods it does not use. More on Wikipedia: [https://en.wikipedia.org/wiki/Interface_segregation_principle].
The Interface Segregation Principle (ISP) advocates for designing interfaces that are specific to client needs. It suggests that no client should be forced to depend on methods it does not use. More on Wikipedia: [https://en.wikipedia.org/wiki/Interface_segregation_principle].


= Dependency Inversion Principle (DIP) =
== Dependency Inversion Principle (DIP) ==
High-level modules should not depend on low-level modules. Both should depend on abstractions. More info: [https://en.wikipedia.org/wiki/Inversion_of_control Inversion of Control].
High-level modules should not depend on low-level modules. Both should depend on abstractions. More info: [https://en.wikipedia.org/wiki/Inversion_of_control Inversion of Control].
= Applying SOLID =
Adhering to SOLID principles can significantly enhance the quality of a codebase and ensure long-term maintainability.
== 1.Requirements and Design == 
Before coding, invest time in understanding the requirements and designing the solution. Use tools like UML diagrams to model relationships and responsibilities.
== 2. Apply SOLID in Small Iterations == 
Break down work into small, manageable tasks. For each task:
* Review if your components respect SRP by checking if they serve only one purpose.
* Ensure extensions follow OCP by designing for extensibility (e.g., using interfaces or abstract classes).
== 3. Focus on Interfaces and Abstractions == 
* Use interfaces or abstract classes to enforce DIP and ISP.
* Avoid coupling high-level modules (e.g., a service class that handles user registration) with low-level implementations (e.g., a specific database library or direct SQL queries).
== 4. Write Tests for Substitutability == 
* Use unit tests to verify that subclasses conform to the behavior defined by their superclasses, ensuring LSP compliance.
== 5. Refactor Regularly == 
Refactor code to correct violations of SOLID principles. For instance:
* Split classes violating SRP.
* Replace tightly coupled dependencies with abstractions.
== 6. Peer Reviews == 
Incorporate code reviews to ensure adherence to SOLID. Team members can spot deviations and suggest improvements. Even though the code creator is primarily responsible for ensuring code quality and cleanliness.
== Verifying Compliance ==
To verify that your code adheres to SOLID principles, employ the following practices:
* '''Automated Tests:''' Ensure robust unit and integration test coverage. Tests reveal dependencies and potential violations of SRP, OCP, and LSP.
* '''Static Code Analysis:'''Use tools like SonarQube or ReSharper to detect code smells and violations of design principles, ensuring they are configured correctly
* '''Metrics:''' Monitor complexity and cohesion metrics. For instance:
** High cohesion and low coupling align with SRP and DIP.
** Large classes or methods may indicate SRP violations.
* '''Feedback Loops:''' Discuss design decisions in team meetings or retrospectives to align on SOLID practices.

Latest revision as of 16:10, 20 January 2025

SOLID

SOLID is an acronym representing a series of principles aimed at promoting more understandable, flexible, and maintainable software development. These principles are widely accepted in object-oriented programming and are essential for reducing complexity, avoiding code smells, and ensuring scalability. The SOLID principles:


While Robert C. Martin, often referred to as "Uncle Bob," is widely recognized as the father of the SOLID principles, it's important to acknowledge the significant contributions of other thought leaders in the field. Barbara Liskov and Jeannette Wing formulated the Liskov Substitution Principle, which underpins the concept of subtype polymorphism in object-oriented programming. Bertrand Meyer introduced the Open/Closed Principle, advocating for software entities to be open for extension but closed for modification. Michael Feathers later contributed by coining the SOLID acronym, helping to popularize these principles.

Single Responsibility Principle (SRP)

The Single Responsibility Principle advocates for a component to have one, and only one, reason to change. This principle warns against a component's purpose being multifaceted—if the description includes "and," it likely serves more than one responsibility and should be refactored into smaller, more focused entities.

Benefits of SRP

Adhering to SRP offers numerous advantages:

  • Simplified Testing: Smaller, isolated components are easier to test.
  • Enhanced Maintainability: Changes in one part of the system have minimal impact on others.
  • Increased Reusability: Focused components can be reused across different parts of the application.

SRP not only makes code cleaner but also aligns with the values of clarity, focus, and accountability.

Open/Closed Principle (OCP)

The Open/Closed Principle is a key tenet of object-oriented design that stipulates that software entities (classes, modules, functions, etc.) should be open for extension but closed for modification. This means that the behavior of a module can be extended without modifying its source code, which helps prevent issues when the system evolves.

Liskov Substitution Principle (LSP)

The Liskov Substitution Principle asserts that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program. In essence, subclasses should not alter the expected behavior of the base class.

Interface Segregation Principle (ISP)

The Interface Segregation Principle (ISP) advocates for designing interfaces that are specific to client needs. It suggests that no client should be forced to depend on methods it does not use. More on Wikipedia: [1].

Dependency Inversion Principle (DIP)

High-level modules should not depend on low-level modules. Both should depend on abstractions. More info: Inversion of Control.


Applying SOLID

Adhering to SOLID principles can significantly enhance the quality of a codebase and ensure long-term maintainability.

1.Requirements and Design

Before coding, invest time in understanding the requirements and designing the solution. Use tools like UML diagrams to model relationships and responsibilities.

2. Apply SOLID in Small Iterations

Break down work into small, manageable tasks. For each task:

  • Review if your components respect SRP by checking if they serve only one purpose.
  • Ensure extensions follow OCP by designing for extensibility (e.g., using interfaces or abstract classes).

3. Focus on Interfaces and Abstractions

  • Use interfaces or abstract classes to enforce DIP and ISP.
  • Avoid coupling high-level modules (e.g., a service class that handles user registration) with low-level implementations (e.g., a specific database library or direct SQL queries).

4. Write Tests for Substitutability

  • Use unit tests to verify that subclasses conform to the behavior defined by their superclasses, ensuring LSP compliance.

5. Refactor Regularly

Refactor code to correct violations of SOLID principles. For instance:

  • Split classes violating SRP.
  • Replace tightly coupled dependencies with abstractions.

6. Peer Reviews

Incorporate code reviews to ensure adherence to SOLID. Team members can spot deviations and suggest improvements. Even though the code creator is primarily responsible for ensuring code quality and cleanliness.

Verifying Compliance

To verify that your code adheres to SOLID principles, employ the following practices:

  • Automated Tests: Ensure robust unit and integration test coverage. Tests reveal dependencies and potential violations of SRP, OCP, and LSP.
  • Static Code Analysis:Use tools like SonarQube or ReSharper to detect code smells and violations of design principles, ensuring they are configured correctly
  • Metrics: Monitor complexity and cohesion metrics. For instance:
    • High cohesion and low coupling align with SRP and DIP.
    • Large classes or methods may indicate SRP violations.
  • Feedback Loops: Discuss design decisions in team meetings or retrospectives to align on SOLID practices.