Jump to content

SOLID: Difference between revisions

From Knowledge Base
Created page with "__TOC__ = 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: {{#mermaid: graph TD SRP-->|One reason to change| SOLID OCP-->|Open for extension, closed for modification| SOLID LSP..."
 
No edit summary
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|alt=Meme of person shooting a football with a golf club into a basket.]]
[[File:Howmanysports.gif|thumb|center|alt=Meme of person shooting a football with a golf club into a basket.]]


= Open/Closed Principle (OCP) =
= Open/Closed Principle (OCP) =

Revision as of 10:24, 17 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.

Meme of person shooting a football with a golf club into a basket.

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.