Jump to content

SOLID

From Wikipedia, the free encyclopedia
(Redirected from Clean Code)

In software programming, SOLID is a mnemonic acronym for five design principles intended to make object-oriented designs more understandable, flexible, and maintainable. Although the SOLID principles apply to any object-oriented design, they can also form a core philosophy for methodologies such as agile development or adaptive software development.[1]

Principles

[edit]

Single responsibility principle

[edit]

Single-responsibility principle (SRP) states that "[t]here should never be more than one reason for a class to change."[2] In other words, every class should have only one responsibility.[3]

Importance

[edit]
  • Maintainability: When classes have a single, well-defined responsibility, they're easier to understand and modify.
  • Testability: It's easier to write unit tests for classes with a single focus.
  • Flexibility: Changes to one responsibility don't affect unrelated parts of the system.[3]

Open–closed principle

[edit]

Open–closed principle (OCP) states that "[s]oftware entities ... should be open for extension, but closed for modification."[4]

Importance

[edit]
  • Extensibility: New features can be added without modifying existing code.
  • Stability: Reduces the risk of introducing bugs when making changes.
  • Flexibility: Adapts to changing requirements more easily.

Liskov substitution principle

[edit]

Liskov substitution principle (LSP) states that "[f]unctions that use pointers or references to base classes must be able to use objects of derived classes without knowing it."[5] See also design by contract.[5]

Importance

[edit]
  • Polymorphism: Enables the use of polymorphic behavior, making code more flexible and reusable.
  • Reliability: Ensures that subclasses adhere to the contract defined by the superclass.
  • Predictability: Guarantees that replacing a superclass object with a subclass object won't break the program.[5]

Interface segregation principle

[edit]

Interface segregation principle (ISP) states that "[c]lients should not be forced to depend upon interfaces that they do not use."[6][7]

Importance

[edit]
  • Decoupling: Reduces dependencies between classes, making the code more modular and maintainable.
  • Flexibility: Allows for more targeted implementations of interfaces.
  • Avoids unnecessary dependencies: Clients don't have to depend on methods they don't use.

Dependency inversion principle

[edit]

Dependency inversion principle (DIP) states to depend upon abstractions, [not] concretes.[8][7]

Importance

[edit]
  • Loose coupling: Reduces dependencies between modules, making the code more flexible and easier to test.
  • Flexibility: Enables changes to implementations without affecting clients.
  • Maintainability: Makes code easier to understand and modify.[8][7]

Origin

[edit]

Software engineer and instructor, Robert C. Martin,[9][10][1] introduced the collection of principles in his 2000 paper Design Principles and Design Patterns about software rot.[10][7]: 2–3  The SOLID acronym was coined around 2004 by Michael Feathers.[11]

See also

[edit]

References

[edit]
  1. ^ a b Metz, Sandi (May 2009). "SOLID Object-Oriented Design". YouTube. Archived from the original on 2021-12-21. Retrieved 2019-08-13. Talk given at the 2009 Gotham Ruby Conference.
  2. ^ "Single Responsibility Principle" (PDF). objectmentor.com. Archived from the original on 2 February 2015.{{cite web}}: CS1 maint: unfit URL (link)
  3. ^ a b Martin, Robert C. (2003). Agile Software Development, Principles, Patterns, and Practices. Prentice Hall. p. 95. ISBN 978-0135974445.
  4. ^ "Open/Closed Principle" (PDF). objectmentor.com. Archived from the original on 5 September 2015.{{cite web}}: CS1 maint: unfit URL (link)
  5. ^ a b c "Liskov Substitution Principle" (PDF). objectmentor.com. Archived from the original on 5 September 2015.{{cite web}}: CS1 maint: unfit URL (link)
  6. ^ "Interface Segregation Principle" (PDF). objectmentor.com. 1996. Archived from the original on 5 September 2015.{{cite web}}: CS1 maint: unfit URL (link)
  7. ^ a b c d Martin, Robert C. (2000). "Design Principles and Design Patterns" (PDF). objectmentor.com. Archived from the original on 2015-09-06.{{cite web}}: CS1 maint: unfit URL (link)
  8. ^ a b "Dependency Inversion Principle" (PDF). objectmentor.com. Archived from the original on 5 September 2015.{{cite web}}: CS1 maint: unfit URL (link)
  9. ^ Martin, Robert C. "Principles Of OOD". ButUncleBob.com. Archived from the original on Sep 10, 2014. Retrieved 2014-07-17.. (Note the reference to "the first five principles", although the acronym is not used in this article.) Dates back to at least 2003.
  10. ^ a b Martin, Robert C. (13 Feb 2009). "Getting a SOLID start". Uncle Bob Consulting LLC (Google Sites). Archived from the original on Sep 17, 2013. Retrieved 2013-08-19.
  11. ^ Martin, Robert (2018). Clean Architecture: A Craftsman's Guide to Software Structure and Design. Prentice Hall. p. 58. ISBN 9780134494166.