SlideShare a Scribd company logo
SOLID PRINCIPLES
with Ruby examples
By Nhan Nguyen (nhanntit)
ROBERT C. MARTIN
“Clean code is not written by
following a set of rules. You
don’t become a software
craftsman by learning a list
of heuristics. Professionalism
and craftsmanship come
from values that drive
disciplines.”
Clean Code: A Handbook of Agile
Software Craftsmanship
By following SOLID design
principles, developers can achieve
a maintainable and extendable
codebase
CONTENT
SINGLE RESPONSIBILITY
PRINCIPLE
A class should have a single
responsibility.
 A class’s responsibility isn’t always
immediately clear.
 Any complicated classes should be divided
into smaller classes that are each
responsible for a particular behaviour,
making it easier to understand and maintain
your codebase.
 The goal of the SR principle is to fight
complexity that creeps in while you’re
developing an application’s logic.
CHECK AN EXAMPLE
Any violation SRP?
VIOLATIONS
1. Handling two tasks:
generate_report!, send_report
2. May be problematic in
expanding in the future: have
to change the logic of the class
REFACTORED
1. Separate classes
2. Changing the logic of the class
without touching the remaining
class
OPEN-CLOSED PRINCIPLE
Modules, classes, methods and
other application entities should be
open for extension but closed for
modification.
 The codebase of software projects is often
modified throughout their lifetimes.
Therefore, a developer’s goal should be to
build a flexible system that is easy to modify
and extend.
 All modules, classes, methods, etc. should
be designed in a modular way, so that we
are able to change the behavior of the
system without changing the source code.
CHECK AN EXAMPLE
Any violation OCP?
VIOLATIONS
1. We’ll have to modify the logger
every time we need to add
additional senders or formatters
REFACTORED
1. Segregated senders and formatters to
separate classes
2. Enabled the addition of new senders and
formatters without having to modify the base
code
LISKOV SUBSTITUTION
PRINCIPLE
Subclasses should add to a base
class’s behaviour, not replace it.
 The principle states that parent instances
should be replaceable with one of their
child instances without creating any
unexpected or incorrect behavior.
 LSP ensures that abstractions are correct,
and helps developers achieve more
reusable code and better organize class
hierarchies
CHECK AN EXAMPLE
Any violation LSP?
VIOLATIONS
1. The child class violates the LSP
principle since it completely
redefines the base class by
returning a string with filtered
data, whereas the base class
returns an array of posts.
REFACTORED
1. Segregated the filtration logic
and the statistics string
generation logic into two
methods: “posts“ and
“formatted_posts“
THE INTERFACE SEGREGATION
PRINCIPLE
Clients shouldn’t depend on
methods they don’t use. Several
client-specific interfaces are better
than one generalized interface.
 Main classes should be segregated into
smaller specific classes, so their clients use
only methods they need.
 We get the interfaces segregated
according to their purpose, so we avoid
“fat” classes and code that’s hard to
maintain.
CHECK AN EXAMPLE
Any violation ISP?
VIOLATIONS
1. Existing the unused methods for
an instance
REFACTORED
1. Segregated in two interfaces
with methods that perform
specific tasks
THE DEPENDENCY INVERSION
PRINCIPLE
High-level modules shouldn’t
depend on low-level modules.
 Abstractions shouldn’t depend on details.
Details depend on abstractions
 The code that follows the LSP and OCP
principles should be readable and contain
clearly separated abstractions. It should also
be extendable, and child classes should be
easily replaceable by other instances of a
base class without breaking the system.
CHECK AN EXAMPLE
Any violation DIP?
VIOLATIONS
1. The classes PdfFormatter and
HtmlFormatter may contain the
logic that refers to other
classes. Thus, we may impact
all the related classes when
modifying the class Printer
REFACTORED
1. High-level object (Printer)
doesn’t depend directly on the
implementation of low-level
objects
2. Our high-level functionality is
separated from all low-level
details, so we’re able to easily
change the low-level logic
without system-wide
implications
CONCLUSION
Applied correctly, SOLID design
recommendations can help you
build system architecture that is
easy to modify and extend over
time
 Saying “Now my code conforms to SOLID
design principles” is the wrong approach.
 Remember that SOLID principles themselves
don’t guarantee great object-oriented
design.
 Apply SOLID principles smartly
RELAX
Solid principles

More Related Content

PPTX
OO Design Principles
Anju Kanjirathingal
 
PPTX
Solid Principles
humayunlkhan
 
PPTX
Learning solid principles using c#
Aditya Kumar Rajan
 
PPT
Oop interfaces
Kaustubh Joshi
 
DOCX
SMU bca sem 4 fall 2014 assignments
solved_assignments
 
PPTX
Soild principles
Avidnyat Chiddarwar
 
PPTX
SOLID_Principles_Explained_Presentation.pptx
SahanaRV2
 
PPTX
Solid Principles
Gaurav Mishra
 
OO Design Principles
Anju Kanjirathingal
 
Solid Principles
humayunlkhan
 
Learning solid principles using c#
Aditya Kumar Rajan
 
Oop interfaces
Kaustubh Joshi
 
SMU bca sem 4 fall 2014 assignments
solved_assignments
 
Soild principles
Avidnyat Chiddarwar
 
SOLID_Principles_Explained_Presentation.pptx
SahanaRV2
 
Solid Principles
Gaurav Mishra
 

Similar to Solid principles (20)

PDF
Solid principle
muhammadali0014
 
PPTX
Solid
Hadi Ehterami
 
PPTX
Software Design Principles (SOLID)
ASIMYILDIZ
 
PPTX
Solid
savithaks2211
 
PPTX
Birth of a developer
Piyush Rahate
 
PPTX
An Introduction to the SOLID Principles
Attila Bertók
 
PPTX
Is your code SOLID enough?
SARCCOM
 
PPTX
SOLID Principles
Surendra Shukla
 
PDF
SOLID design principles in Ruby
Anil Wadghule
 
PPTX
Sec1_SOLID Principles_Software Engineering.pptx
HebaSamy22
 
PDF
Solid OO & Clean Coding is essential to successful Agile development
Simon Gould
 
PPTX
¿Qué tan s.o.l.i.d. es tú código
Luis Alexander Aldazabal Gil
 
PDF
Understanding SOLID Principles in OOP programming
AntonelaAniLeka
 
PPTX
GDSC - SOLID Principles session.pptx
AaliyanShaikh
 
PDF
Solid - OOD Principles
Creditas
 
PPTX
Establishing a SOLID Foundation
Cameron Presley
 
PPTX
SOLID Principles in OOPS ooooooooo.pptx
banjaaring
 
PDF
L22 Design Principles
Ólafur Andri Ragnarsson
 
PDF
Solid principes
Steven Ndaye
 
PPTX
Object Oriented Design SOLID Principles
rainynovember12
 
Solid principle
muhammadali0014
 
Software Design Principles (SOLID)
ASIMYILDIZ
 
Birth of a developer
Piyush Rahate
 
An Introduction to the SOLID Principles
Attila Bertók
 
Is your code SOLID enough?
SARCCOM
 
SOLID Principles
Surendra Shukla
 
SOLID design principles in Ruby
Anil Wadghule
 
Sec1_SOLID Principles_Software Engineering.pptx
HebaSamy22
 
Solid OO & Clean Coding is essential to successful Agile development
Simon Gould
 
¿Qué tan s.o.l.i.d. es tú código
Luis Alexander Aldazabal Gil
 
Understanding SOLID Principles in OOP programming
AntonelaAniLeka
 
GDSC - SOLID Principles session.pptx
AaliyanShaikh
 
Solid - OOD Principles
Creditas
 
Establishing a SOLID Foundation
Cameron Presley
 
SOLID Principles in OOPS ooooooooo.pptx
banjaaring
 
L22 Design Principles
Ólafur Andri Ragnarsson
 
Solid principes
Steven Ndaye
 
Object Oriented Design SOLID Principles
rainynovember12
 
Ad

More from Nhan Nguyen (6)

PDF
Scrum Framework
Nhan Nguyen
 
PDF
Agile planning and monitoring
Nhan Nguyen
 
PPTX
Product quality in agile project
Nhan Nguyen
 
PPT
Retrospective in action
Nhan Nguyen
 
PDF
SCRUM and Poker Game In Agile Project
Nhan Nguyen
 
PDF
Continuous integration - CI
Nhan Nguyen
 
Scrum Framework
Nhan Nguyen
 
Agile planning and monitoring
Nhan Nguyen
 
Product quality in agile project
Nhan Nguyen
 
Retrospective in action
Nhan Nguyen
 
SCRUM and Poker Game In Agile Project
Nhan Nguyen
 
Continuous integration - CI
Nhan Nguyen
 
Ad

Recently uploaded (20)

PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Doc9.....................................
SofiaCollazos
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
The Future of Artificial Intelligence (AI)
Mukul
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 

Solid principles

  • 1. SOLID PRINCIPLES with Ruby examples By Nhan Nguyen (nhanntit)
  • 2. ROBERT C. MARTIN “Clean code is not written by following a set of rules. You don’t become a software craftsman by learning a list of heuristics. Professionalism and craftsmanship come from values that drive disciplines.” Clean Code: A Handbook of Agile Software Craftsmanship
  • 3. By following SOLID design principles, developers can achieve a maintainable and extendable codebase CONTENT
  • 4. SINGLE RESPONSIBILITY PRINCIPLE A class should have a single responsibility.  A class’s responsibility isn’t always immediately clear.  Any complicated classes should be divided into smaller classes that are each responsible for a particular behaviour, making it easier to understand and maintain your codebase.  The goal of the SR principle is to fight complexity that creeps in while you’re developing an application’s logic.
  • 5. CHECK AN EXAMPLE Any violation SRP?
  • 6. VIOLATIONS 1. Handling two tasks: generate_report!, send_report 2. May be problematic in expanding in the future: have to change the logic of the class
  • 7. REFACTORED 1. Separate classes 2. Changing the logic of the class without touching the remaining class
  • 8. OPEN-CLOSED PRINCIPLE Modules, classes, methods and other application entities should be open for extension but closed for modification.  The codebase of software projects is often modified throughout their lifetimes. Therefore, a developer’s goal should be to build a flexible system that is easy to modify and extend.  All modules, classes, methods, etc. should be designed in a modular way, so that we are able to change the behavior of the system without changing the source code.
  • 9. CHECK AN EXAMPLE Any violation OCP?
  • 10. VIOLATIONS 1. We’ll have to modify the logger every time we need to add additional senders or formatters
  • 11. REFACTORED 1. Segregated senders and formatters to separate classes 2. Enabled the addition of new senders and formatters without having to modify the base code
  • 12. LISKOV SUBSTITUTION PRINCIPLE Subclasses should add to a base class’s behaviour, not replace it.  The principle states that parent instances should be replaceable with one of their child instances without creating any unexpected or incorrect behavior.  LSP ensures that abstractions are correct, and helps developers achieve more reusable code and better organize class hierarchies
  • 13. CHECK AN EXAMPLE Any violation LSP?
  • 14. VIOLATIONS 1. The child class violates the LSP principle since it completely redefines the base class by returning a string with filtered data, whereas the base class returns an array of posts.
  • 15. REFACTORED 1. Segregated the filtration logic and the statistics string generation logic into two methods: “posts“ and “formatted_posts“
  • 16. THE INTERFACE SEGREGATION PRINCIPLE Clients shouldn’t depend on methods they don’t use. Several client-specific interfaces are better than one generalized interface.  Main classes should be segregated into smaller specific classes, so their clients use only methods they need.  We get the interfaces segregated according to their purpose, so we avoid “fat” classes and code that’s hard to maintain.
  • 17. CHECK AN EXAMPLE Any violation ISP?
  • 18. VIOLATIONS 1. Existing the unused methods for an instance
  • 19. REFACTORED 1. Segregated in two interfaces with methods that perform specific tasks
  • 20. THE DEPENDENCY INVERSION PRINCIPLE High-level modules shouldn’t depend on low-level modules.  Abstractions shouldn’t depend on details. Details depend on abstractions  The code that follows the LSP and OCP principles should be readable and contain clearly separated abstractions. It should also be extendable, and child classes should be easily replaceable by other instances of a base class without breaking the system.
  • 21. CHECK AN EXAMPLE Any violation DIP?
  • 22. VIOLATIONS 1. The classes PdfFormatter and HtmlFormatter may contain the logic that refers to other classes. Thus, we may impact all the related classes when modifying the class Printer
  • 23. REFACTORED 1. High-level object (Printer) doesn’t depend directly on the implementation of low-level objects 2. Our high-level functionality is separated from all low-level details, so we’re able to easily change the low-level logic without system-wide implications
  • 24. CONCLUSION Applied correctly, SOLID design recommendations can help you build system architecture that is easy to modify and extend over time  Saying “Now my code conforms to SOLID design principles” is the wrong approach.  Remember that SOLID principles themselves don’t guarantee great object-oriented design.  Apply SOLID principles smartly
  • 25. RELAX