SlideShare a Scribd company logo
SOLID Principles
Every developer
should know
TOAN NGUYEN
TO A N - H U U . N G U Y E N @ KO L E K TO . N L
NGUYENTOANUIT
Agenda
What are SOLID?
Why should we care?
How can we apply SOLID principles?
◦ S – Single responsibility principle
◦ O – Open–closed principle
◦ L – Liskov substitution principle
◦ I – Interface segregation principle
◦ D – Dependency inversion principle
SOLID - TOAN NGUYEN
About me
Nguyen Huu Toan
Senior Dev – skype nguyentoanuit@gmail.com
SWGLayer team
https://app.kolekto.nl
SOLID - TOAN NGUYEN
What is SOLID?
“It is not enough for code to work.”
- Robert C. Martin
SOLID is five basic principles which help us write clean code
◦ Understandable
◦ Easier to maintain
◦ Easier to extend
The abbreviation SOLID was introduced by Michael Feathers
But the concept of SOLID principles were first introduced by Robert C. Martin
in his book, Design Principles and Design Patterns.
So let see how SOLID can help us be stronger
SOLID - TOAN NGUYEN
Why should
we care?
BUT WAIT A MINUTE!!!
WHY SHOULD WE NEED
TO APPLY IT?
SOLID - TOAN NGUYEN
Why should we care?
Can we just make it work without following these principles?
Finish the business rules/PBI and delivery functionality to
client are our main task, why do we need to care about these
annoying rules/principles?
SOLID - TOAN NGUYEN
Why should we care?
Yes, of-course.
BUT..... the functionality that you create today has a good
chance of changing in the next release.
The more you care about good code today, the more you are
loved by your teammate later
SOLID - TOAN NGUYEN
Why should we care?
So I will make it work first and refactor later on?
SOLID - TOAN NGUYEN
Why should we care?
Yes, sure! But it will be very expensive.
SOLID - TOAN NGUYEN
Why should we care?
“People will forget how fast you did a job, but they remember
how well you did it!”
- iDev
SOLID - TOAN NGUYEN
How can we
apply SOLID
principles?
S – Single responsibility principle (SRP)
O – Open–closed principle (OCP)
L – Liskov substitution principle (LSP)
I – Interface segregation principle (ISP)
D – Dependency inversion principle (DIP)
SOLID - TOAN NGUYEN
S – Single responsibility principle
“A class should only have a single responsibility”
So that, a class should have one and only one reason to change.
SOLID - TOAN NGUYEN
S – Single responsibility principle
public class Production
{
public Guid Id { get; set; }
public string Title { get; set; }
public string ShortDescription { get; set; }
public string ImageUrl { get; set; }
public int RemainItems { get; set; }
public string CheckStatus()
{
if (RemainItems > 500)
{
return "There are a lot of remaining items";
}
else if (RemainItems <= 500 && RemainItems > 200)
{
return "Warning: We should buy more";
}
else
{
return "Red warning: We need by more";
}
}
}
SOLID - TOAN NGUYEN
Inventory
business
S – Single responsibility principle
public void SendEmail(EmailData emailData)
{
var email = new
{
from = emailData.From,
to = emailData.To,
message = emailData.Message
};
graph.sendEmail(email);
}
SOLID - TOAN NGUYEN
S – Single responsibility principle
SOLID - TOAN NGUYEN
public void SendEmail(EmailData emailData, bool generateAttachment)
{
var email = new GraphEmail
{
from = emailData.From,
to = emailData.To,
message = emailData.Message
};
if (generateAttachment)
{
var document = new
{
title = emailData.AttachmentName,
content = pdfGenerator.GeneratePDF(emailData.Message)
};
email.attachment = document;
}
graph.sendEmail(email);
}
Generate attachment is not
a job of send email function
Avoid violate SRP
1. Long method
2. Boolean flag
3. Class (service) serve many operations
4. Always ask why is the responsibility of class/service/module when extending
SOLID - TOAN NGUYEN
Benefits of SRP
1. Easy to follow/understand
2. Maintainable
3. Test easily
4. Easy to extend
SOLID - TOAN NGUYEN
Open–closed principle
Entities should be open for extension, but closed for modification.
SOLID - TOAN NGUYEN
Open–closed principle
SOLID - TOAN NGUYEN
Open–closed principle
SOLID - TOAN NGUYEN
Open–closed principle
SOLID - TOAN NGUYEN
User form
Open–closed principle
SOLID - TOAN NGUYEN
Is it fully follow OCP?
Open–closed principle
As application grow up, a lot of new behaviors of the form will be added such as
◦ Handle dirty form
◦ Confirmed/warning message
◦ User only can view data, can’t make a change in the form
◦ Show overlay when data is loading
◦ more and more …..
SOLID - TOAN NGUYEN
Open–closed principle
SOLID - TOAN NGUYEN
Open–closed principle
Composition instead of inheritance
SOLID - TOAN NGUYEN
<form>
<FormContext.Provider value={setupContextForm()}>
<Header />
<DirtyCheck />
<BaseWarningMessage />
<ConfirmMessage />
<Messages />
<BaseValidation />
<UserFormFields />
<FormButtons />
....
</FormContext.Provider>
</form>
Benefits of OCP
1. Reusable and maintainable
2. Avoid regression bugs
3. Extend easily and quickly
SOLID - TOAN NGUYEN
Avoid violate OCP
1. Using inheritance
2. Using Composition
3. Using interface
SOLID - TOAN NGUYEN
Liskov substitution principle
Let Φ(x) be a property provable about objects x of type T. Then Φ(y)should be true for
objects y of type S where S is a subtype of T.
Developer language
"Objects in a program should be replaceable with instances of their subtypes without
altering the correctness of that program.“
Related to???
Open – close principle
SOLID - TOAN NGUYEN
Liskov substitution principle
SOLID - TOAN NGUYEN
public class Airplane
{
public string Name { get; set; }
public virtual void TakeOff()
{
Console.WriteLine("Hello madam and gentleman, we are taking off");
}
public virtual void Landdown()
{
Console.WriteLine("Hello madam and gentleman, we are landing");
}
public virtual void Up()
{
Console.WriteLine("Air plane go up");
}
public virtual void Down()
{
Console.WriteLine("Air plane go down");
}
}
public class TraditionalAirPlane : Airplane
{ public TraditionalAirPlane()
{
Name = “Traditional one";
}
}
Liskov substitution principle
SOLID - TOAN NGUYEN
static void Main(string[] args)
{
var airPlane = new BoeingB52();
var pilot = new Pilot(airPlane);
pilot.HandleAirePlane();
}
Liskov substitution principle
SOLID - TOAN NGUYEN
public class SmartAirplane : Airplane
{
public bool AirPlaneUp { get; set; }
public bool OffAutoDetection { get; set; }
public SmartAirplane()
{
Name = "I am smart air plane" ;
}
public override void Up()
{
if (AirPlaneUp && !OffAutoDetection)
{
throw new System. Exception ("No worry, I will control it");
}
base.Up();
}
public void TurnOffAutoDetect()
{
OffAutoDetection = true;
}
}
Liskov substitution principle
SOLID - TOAN NGUYEN
static void Main(string[] args)
{
var airPlane = new SmartAirePlane ();
var pilot = new Pilot(airPlane);
airPlane.AirPlaneUp = true;
pilot.HandleAirePlane();
}
Benefits of LSP
Avoid unexpected behavior of software
SOLID - TOAN NGUYEN
Avoid violate LSP
1. No easy way to detect the violation
2. Review code
3. Shouldn’t implement stricter validation than implementation of superclass
4. Shouldn’t break superclass behavior
5. Should run test case (unit test) for all subclass instances
SOLID - TOAN NGUYEN
Interface segregation principle
“Many client-specific interfaces are better than one general-purpose interface.”
Similar??
SRP
SOLID - TOAN NGUYEN
Interface segregation principle
public interface IEmailService
{
void SendEmail(EmailData emailData);
}
SOLID - TOAN NGUYEN
public class GraphEmailService : IEmailService
{
dynamic graph = new { };
public void SendEmail(EmailData emailData)
{
var email = new
{
from = emailData.From,
to = emailData.To,
message = emailData.Message
};
graph.sendEmail(email);
}
}
public class SendGridEmailService : IEmailService
{
dynamic sendGrid = new { };
public void SendEmail(EmailData emailData)
{
var email = new
{
from = emailData.From,
to = emailData.To,
message = emailData.Message
};
sendGrid.sendEmail(email);
}
}
Interface segregation principle
SOLID - TOAN NGUYEN
Interface segregation principle
SOLID - TOAN NGUYEN
Benefits of ISP
Similar Single responsibility
SOLID - TOAN NGUYEN
Avoid violate ISP
1. Large interface (flat interface)
2. throw new NotImplementedException();
SOLID - TOAN NGUYEN
Dependency inversion principle
1. High-level modules should not depend on low-level modules. Both should depend on
abstractions.
2. Abstractions should not depend on details. Details should depend on abstractions.
Related to???
OCP & LSP
SOLID - TOAN NGUYEN
Benefits of DIP
Decoupling between
1. Abstractions and details
2. High level and low level
SOLID - TOAN NGUYEN
Distinguish between DIP, IoC and DI
1. DIP: Dependency inversion principle is about decoupling
2. IoC: Inversion of control is a design principle used by framework libraries that allow the
framework to regain and instance of class during program execution. Or could be referred by
"Hollywood Principle: Don't call us, we'll call you"
3. DI: Dependency Injection is an implementation of IoC. Another implementation of IoC is
service locator
SOLID - TOAN NGUYEN
Q&A
SOLID - TOAN NGUYEN
Thanks you!
SOLID - TOAN NGUYEN

More Related Content

What's hot

Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#
Aditya Kumar Rajan
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
NexThoughts Technologies
 
Solid design principles
Solid design principlesSolid design principles
Solid design principles
Mahmoud Asadi
 
SOLID Design Principles applied in Java
SOLID Design Principles applied in JavaSOLID Design Principles applied in Java
SOLID Design Principles applied in Java
Ionut Bilica
 
Single Responsibility Principle
Single Responsibility PrincipleSingle Responsibility Principle
Single Responsibility Principle
Eyal Golan
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
humayunlkhan
 
SOLID & IoC Principles
SOLID & IoC PrinciplesSOLID & IoC Principles
SOLID & IoC Principles
Pavlo Hodysh
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
Thiago Dos Santos Hora
 
The Single Responsibility Principle
The Single Responsibility PrincipleThe Single Responsibility Principle
The Single Responsibility Principle
Lars-Erik Kindblad
 
Open Closed Principle kata
Open Closed Principle kataOpen Closed Principle kata
Open Closed Principle kata
Paul Blundell
 
S.O.L.I.D. Principles for Software Architects
S.O.L.I.D. Principles for Software ArchitectsS.O.L.I.D. Principles for Software Architects
S.O.L.I.D. Principles for Software Architects
Ricardo Wilkins
 
SOLID Principles and The Clean Architecture
SOLID Principles and The Clean ArchitectureSOLID Principles and The Clean Architecture
SOLID Principles and The Clean Architecture
Mohamed Galal
 
SOLID
SOLIDSOLID
What is Dependency Injection in Spring Boot | Edureka
What is Dependency Injection in Spring Boot | EdurekaWhat is Dependency Injection in Spring Boot | Edureka
What is Dependency Injection in Spring Boot | Edureka
Edureka!
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Design Patterns: From STUPID to SOLID code
Design Patterns: From STUPID to SOLID codeDesign Patterns: From STUPID to SOLID code
Design Patterns: From STUPID to SOLID code
Paulo Gandra de Sousa
 

What's hot (20)

Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
Solid design principles
Solid design principlesSolid design principles
Solid design principles
 
SOLID Design Principles applied in Java
SOLID Design Principles applied in JavaSOLID Design Principles applied in Java
SOLID Design Principles applied in Java
 
Single Responsibility Principle
Single Responsibility PrincipleSingle Responsibility Principle
Single Responsibility Principle
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
SOLID & IoC Principles
SOLID & IoC PrinciplesSOLID & IoC Principles
SOLID & IoC Principles
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
The Single Responsibility Principle
The Single Responsibility PrincipleThe Single Responsibility Principle
The Single Responsibility Principle
 
Open Closed Principle kata
Open Closed Principle kataOpen Closed Principle kata
Open Closed Principle kata
 
S.O.L.I.D. Principles for Software Architects
S.O.L.I.D. Principles for Software ArchitectsS.O.L.I.D. Principles for Software Architects
S.O.L.I.D. Principles for Software Architects
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
SOLID Principles and The Clean Architecture
SOLID Principles and The Clean ArchitectureSOLID Principles and The Clean Architecture
SOLID Principles and The Clean Architecture
 
SOLID
SOLIDSOLID
SOLID
 
What is Dependency Injection in Spring Boot | Edureka
What is Dependency Injection in Spring Boot | EdurekaWhat is Dependency Injection in Spring Boot | Edureka
What is Dependency Injection in Spring Boot | Edureka
 
Solid Principle
Solid PrincipleSolid Principle
Solid Principle
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Design Patterns: From STUPID to SOLID code
Design Patterns: From STUPID to SOLID codeDesign Patterns: From STUPID to SOLID code
Design Patterns: From STUPID to SOLID code
 

Similar to Solid principles

"S"OLID Principles - Single responsibility principle
"S"OLID Principles - Single responsibility principle"S"OLID Principles - Single responsibility principle
"S"OLID Principles - Single responsibility principle
Nishant Upadhyay
 
An Introduction to the SOLID Principles
An Introduction to the SOLID PrinciplesAn Introduction to the SOLID Principles
An Introduction to the SOLID Principles
Attila Bertók
 
Pavlo Zhdanov "Mastering solid and base principles for software design"
Pavlo Zhdanov "Mastering solid and base principles for software design"Pavlo Zhdanov "Mastering solid and base principles for software design"
Pavlo Zhdanov "Mastering solid and base principles for software design"
LogeekNightUkraine
 
L22 Design Principles
L22 Design PrinciplesL22 Design Principles
L22 Design Principles
Ólafur Andri Ragnarsson
 
SOLID
SOLIDSOLID
principles of object oriented class design
principles of object oriented class designprinciples of object oriented class design
principles of object oriented class design
Neetu Mishra
 
Solid js
Solid jsSolid js
Solid js
jonathanfmills
 
Writing Maintainable Software Using SOLID Principles
Writing Maintainable Software Using SOLID PrinciplesWriting Maintainable Software Using SOLID Principles
Writing Maintainable Software Using SOLID Principles
Doug Jones
 
Design principle vs design patterns
Design principle vs design patternsDesign principle vs design patterns
Design principle vs design patterns
Prabhakar Sharma
 
Solid Principles
Solid PrinciplesSolid Principles
Solid PrinciplesHitheshh
 
The good, the bad and the SOLID
The good, the bad and the SOLIDThe good, the bad and the SOLID
The good, the bad and the SOLID
Frikkie van Biljon
 
Object Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;sObject Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;s
vivek p s
 
SOLID principles-Present
SOLID principles-PresentSOLID principles-Present
SOLID principles-PresentQuang Nguyen
 
Ood and solid principles
Ood and solid principlesOod and solid principles
Ood and solid principles
Avinash Kadam
 
L07 Design Principles
L07 Design PrinciplesL07 Design Principles
L07 Design Principles
Ólafur Andri Ragnarsson
 
Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)
Dave Hulbert
 
The maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID PrinciplesThe maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID Principles
Muhammad Raza
 
2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#
Daniel Fisher
 
Object Oriented Design SOLID Principles
Object Oriented Design SOLID PrinciplesObject Oriented Design SOLID Principles
Object Oriented Design SOLID Principles
rainynovember12
 

Similar to Solid principles (20)

"S"OLID Principles - Single responsibility principle
"S"OLID Principles - Single responsibility principle"S"OLID Principles - Single responsibility principle
"S"OLID Principles - Single responsibility principle
 
An Introduction to the SOLID Principles
An Introduction to the SOLID PrinciplesAn Introduction to the SOLID Principles
An Introduction to the SOLID Principles
 
Pavlo Zhdanov "Mastering solid and base principles for software design"
Pavlo Zhdanov "Mastering solid and base principles for software design"Pavlo Zhdanov "Mastering solid and base principles for software design"
Pavlo Zhdanov "Mastering solid and base principles for software design"
 
L22 Design Principles
L22 Design PrinciplesL22 Design Principles
L22 Design Principles
 
SOLID
SOLIDSOLID
SOLID
 
principles of object oriented class design
principles of object oriented class designprinciples of object oriented class design
principles of object oriented class design
 
Solid js
Solid jsSolid js
Solid js
 
Writing Maintainable Software Using SOLID Principles
Writing Maintainable Software Using SOLID PrinciplesWriting Maintainable Software Using SOLID Principles
Writing Maintainable Software Using SOLID Principles
 
Oop principles
Oop principlesOop principles
Oop principles
 
Design principle vs design patterns
Design principle vs design patternsDesign principle vs design patterns
Design principle vs design patterns
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
The good, the bad and the SOLID
The good, the bad and the SOLIDThe good, the bad and the SOLID
The good, the bad and the SOLID
 
Object Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;sObject Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;s
 
SOLID principles-Present
SOLID principles-PresentSOLID principles-Present
SOLID principles-Present
 
Ood and solid principles
Ood and solid principlesOod and solid principles
Ood and solid principles
 
L07 Design Principles
L07 Design PrinciplesL07 Design Principles
L07 Design Principles
 
Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)
 
The maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID PrinciplesThe maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID Principles
 
2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#
 
Object Oriented Design SOLID Principles
Object Oriented Design SOLID PrinciplesObject Oriented Design SOLID Principles
Object Oriented Design SOLID Principles
 

Recently uploaded

How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 

Recently uploaded (20)

How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 

Solid principles

  • 1. SOLID Principles Every developer should know TOAN NGUYEN TO A N - H U U . N G U Y E N @ KO L E K TO . N L NGUYENTOANUIT
  • 2. Agenda What are SOLID? Why should we care? How can we apply SOLID principles? ◦ S – Single responsibility principle ◦ O – Open–closed principle ◦ L – Liskov substitution principle ◦ I – Interface segregation principle ◦ D – Dependency inversion principle SOLID - TOAN NGUYEN
  • 3. About me Nguyen Huu Toan Senior Dev – skype nguyentoanuit@gmail.com SWGLayer team https://app.kolekto.nl SOLID - TOAN NGUYEN
  • 4. What is SOLID? “It is not enough for code to work.” - Robert C. Martin SOLID is five basic principles which help us write clean code ◦ Understandable ◦ Easier to maintain ◦ Easier to extend The abbreviation SOLID was introduced by Michael Feathers But the concept of SOLID principles were first introduced by Robert C. Martin in his book, Design Principles and Design Patterns. So let see how SOLID can help us be stronger SOLID - TOAN NGUYEN
  • 5. Why should we care? BUT WAIT A MINUTE!!! WHY SHOULD WE NEED TO APPLY IT? SOLID - TOAN NGUYEN
  • 6. Why should we care? Can we just make it work without following these principles? Finish the business rules/PBI and delivery functionality to client are our main task, why do we need to care about these annoying rules/principles? SOLID - TOAN NGUYEN
  • 7. Why should we care? Yes, of-course. BUT..... the functionality that you create today has a good chance of changing in the next release. The more you care about good code today, the more you are loved by your teammate later SOLID - TOAN NGUYEN
  • 8. Why should we care? So I will make it work first and refactor later on? SOLID - TOAN NGUYEN
  • 9. Why should we care? Yes, sure! But it will be very expensive. SOLID - TOAN NGUYEN
  • 10. Why should we care? “People will forget how fast you did a job, but they remember how well you did it!” - iDev SOLID - TOAN NGUYEN
  • 11. How can we apply SOLID principles? S – Single responsibility principle (SRP) O – Open–closed principle (OCP) L – Liskov substitution principle (LSP) I – Interface segregation principle (ISP) D – Dependency inversion principle (DIP) SOLID - TOAN NGUYEN
  • 12. S – Single responsibility principle “A class should only have a single responsibility” So that, a class should have one and only one reason to change. SOLID - TOAN NGUYEN
  • 13. S – Single responsibility principle public class Production { public Guid Id { get; set; } public string Title { get; set; } public string ShortDescription { get; set; } public string ImageUrl { get; set; } public int RemainItems { get; set; } public string CheckStatus() { if (RemainItems > 500) { return "There are a lot of remaining items"; } else if (RemainItems <= 500 && RemainItems > 200) { return "Warning: We should buy more"; } else { return "Red warning: We need by more"; } } } SOLID - TOAN NGUYEN Inventory business
  • 14. S – Single responsibility principle public void SendEmail(EmailData emailData) { var email = new { from = emailData.From, to = emailData.To, message = emailData.Message }; graph.sendEmail(email); } SOLID - TOAN NGUYEN
  • 15. S – Single responsibility principle SOLID - TOAN NGUYEN public void SendEmail(EmailData emailData, bool generateAttachment) { var email = new GraphEmail { from = emailData.From, to = emailData.To, message = emailData.Message }; if (generateAttachment) { var document = new { title = emailData.AttachmentName, content = pdfGenerator.GeneratePDF(emailData.Message) }; email.attachment = document; } graph.sendEmail(email); } Generate attachment is not a job of send email function
  • 16. Avoid violate SRP 1. Long method 2. Boolean flag 3. Class (service) serve many operations 4. Always ask why is the responsibility of class/service/module when extending SOLID - TOAN NGUYEN
  • 17. Benefits of SRP 1. Easy to follow/understand 2. Maintainable 3. Test easily 4. Easy to extend SOLID - TOAN NGUYEN
  • 18. Open–closed principle Entities should be open for extension, but closed for modification. SOLID - TOAN NGUYEN
  • 21. Open–closed principle SOLID - TOAN NGUYEN User form
  • 22. Open–closed principle SOLID - TOAN NGUYEN Is it fully follow OCP?
  • 23. Open–closed principle As application grow up, a lot of new behaviors of the form will be added such as ◦ Handle dirty form ◦ Confirmed/warning message ◦ User only can view data, can’t make a change in the form ◦ Show overlay when data is loading ◦ more and more ….. SOLID - TOAN NGUYEN
  • 25. Open–closed principle Composition instead of inheritance SOLID - TOAN NGUYEN <form> <FormContext.Provider value={setupContextForm()}> <Header /> <DirtyCheck /> <BaseWarningMessage /> <ConfirmMessage /> <Messages /> <BaseValidation /> <UserFormFields /> <FormButtons /> .... </FormContext.Provider> </form>
  • 26. Benefits of OCP 1. Reusable and maintainable 2. Avoid regression bugs 3. Extend easily and quickly SOLID - TOAN NGUYEN
  • 27. Avoid violate OCP 1. Using inheritance 2. Using Composition 3. Using interface SOLID - TOAN NGUYEN
  • 28. Liskov substitution principle Let Φ(x) be a property provable about objects x of type T. Then Φ(y)should be true for objects y of type S where S is a subtype of T. Developer language "Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.“ Related to??? Open – close principle SOLID - TOAN NGUYEN
  • 29. Liskov substitution principle SOLID - TOAN NGUYEN public class Airplane { public string Name { get; set; } public virtual void TakeOff() { Console.WriteLine("Hello madam and gentleman, we are taking off"); } public virtual void Landdown() { Console.WriteLine("Hello madam and gentleman, we are landing"); } public virtual void Up() { Console.WriteLine("Air plane go up"); } public virtual void Down() { Console.WriteLine("Air plane go down"); } } public class TraditionalAirPlane : Airplane { public TraditionalAirPlane() { Name = “Traditional one"; } }
  • 30. Liskov substitution principle SOLID - TOAN NGUYEN static void Main(string[] args) { var airPlane = new BoeingB52(); var pilot = new Pilot(airPlane); pilot.HandleAirePlane(); }
  • 31. Liskov substitution principle SOLID - TOAN NGUYEN public class SmartAirplane : Airplane { public bool AirPlaneUp { get; set; } public bool OffAutoDetection { get; set; } public SmartAirplane() { Name = "I am smart air plane" ; } public override void Up() { if (AirPlaneUp && !OffAutoDetection) { throw new System. Exception ("No worry, I will control it"); } base.Up(); } public void TurnOffAutoDetect() { OffAutoDetection = true; } }
  • 32. Liskov substitution principle SOLID - TOAN NGUYEN static void Main(string[] args) { var airPlane = new SmartAirePlane (); var pilot = new Pilot(airPlane); airPlane.AirPlaneUp = true; pilot.HandleAirePlane(); }
  • 33. Benefits of LSP Avoid unexpected behavior of software SOLID - TOAN NGUYEN
  • 34. Avoid violate LSP 1. No easy way to detect the violation 2. Review code 3. Shouldn’t implement stricter validation than implementation of superclass 4. Shouldn’t break superclass behavior 5. Should run test case (unit test) for all subclass instances SOLID - TOAN NGUYEN
  • 35. Interface segregation principle “Many client-specific interfaces are better than one general-purpose interface.” Similar?? SRP SOLID - TOAN NGUYEN
  • 36. Interface segregation principle public interface IEmailService { void SendEmail(EmailData emailData); } SOLID - TOAN NGUYEN public class GraphEmailService : IEmailService { dynamic graph = new { }; public void SendEmail(EmailData emailData) { var email = new { from = emailData.From, to = emailData.To, message = emailData.Message }; graph.sendEmail(email); } } public class SendGridEmailService : IEmailService { dynamic sendGrid = new { }; public void SendEmail(EmailData emailData) { var email = new { from = emailData.From, to = emailData.To, message = emailData.Message }; sendGrid.sendEmail(email); } }
  • 39. Benefits of ISP Similar Single responsibility SOLID - TOAN NGUYEN
  • 40. Avoid violate ISP 1. Large interface (flat interface) 2. throw new NotImplementedException(); SOLID - TOAN NGUYEN
  • 41. Dependency inversion principle 1. High-level modules should not depend on low-level modules. Both should depend on abstractions. 2. Abstractions should not depend on details. Details should depend on abstractions. Related to??? OCP & LSP SOLID - TOAN NGUYEN
  • 42. Benefits of DIP Decoupling between 1. Abstractions and details 2. High level and low level SOLID - TOAN NGUYEN
  • 43. Distinguish between DIP, IoC and DI 1. DIP: Dependency inversion principle is about decoupling 2. IoC: Inversion of control is a design principle used by framework libraries that allow the framework to regain and instance of class during program execution. Or could be referred by "Hollywood Principle: Don't call us, we'll call you" 3. DI: Dependency Injection is an implementation of IoC. Another implementation of IoC is service locator SOLID - TOAN NGUYEN
  • 45. Thanks you! SOLID - TOAN NGUYEN

Editor's Notes

  1. It doesn't mean that a class should have one property and one method. All the methods and properties should all work towards the same goal
  2. Later, we need to check status of a production to decide buy more or not
  3. Later we want to support send email with attachment
  4. We have base form like this
  5. So we can add more form like product, invoice without changing base form
  6. How can se communicate between these components? => Pub/Sub pattern
  7. Why we have interface here? If you think of application level, for example your application need support send email 1. we will implement email service which use graph api for example, 2. later we also need to support SendGrid as send mail service as well. So by implement interface IEmailService we can extend application easily by don’t modify the service using graph api.
  8. A company’s broken this rule and get a very big problem now. => Boeing
  9. Easy to follow/understand Maintainable Test easily
  10. The Open/Closed Principle required a software component to be open for extension, but closed for modification. You can achieve that by introducing interfaces for which you can provide different implementations. The interface itself is closed for modification, and you can easily extend it by providing a new interface implementation. Your implementations should follow the Liskov Substitution Principle so that you can replace them with other implementations of the same interface without breaking your application.
  11. Test easily