Is your code solid
-
Upload
nathan-gloyn -
Category
Technology
-
view
2.513 -
download
4
description
Transcript of Is your code solid
![Page 1: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/1.jpg)
IS YOUR CODE SOLID?Principles behind good software design
@NathanGloyn
nathangloyn
Design Code Release
![Page 2: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/2.jpg)
Agenda
The origin of S.O.L.I.D What is S.O.L.I.D? Design Smells The principles S.O.L.I.D code is... Summary Questions Resources
![Page 3: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/3.jpg)
The origin of S.O.L.I.D
Who is this man?
![Page 4: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/4.jpg)
Principles, Patterns, and Practices
Published in 2002 First book to mention
SOLID C# edition published
2005 Both books
considered seminal works on SOLID
The origin of S.O.L.I.D
![Page 5: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/5.jpg)
What is S.O.L.I.D?
Principles about class design To be considered, not rules blindly
applied Made up of acronyms Minimise design smells Easier to evolve code
![Page 6: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/6.jpg)
Design Smells
Rigidity Fragility Immobility Viscosity Needless Complexity Needless Repetition Opacity
![Page 7: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/7.jpg)
The principles
S ingle Responsibility Principle –> SRP
O pen/Closed Principle –>OCP L iskov Substitution Principle –>
LSP I nterface Segregation Principle –>
ISP D ependency Inversion Principle –>
DIP
![Page 8: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/8.jpg)
Single Responsibility Principle
What is the principle? A class should have only one reason to
change.
Why should you use it? Helps with separation of concerns Increases cohesion Reduces coupling Simplifies the code, making it more readable Makes future changes easier
The principles
![Page 9: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/9.jpg)
Single Responsibility Principle
Code
The principles
![Page 10: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/10.jpg)
Interface Segregation Principle
What is the principle? Clients should not be forced to depend
upon interfaces that they do not use Avoid “fat” interfaces
Why should you use it? Reduces coupling Implementers only use bits they need. Helps reduce dependencies
The principles
![Page 11: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/11.jpg)
Interface Segregation Principle
Code
The principles
![Page 12: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/12.jpg)
Dependency Inversion Principle
What is the principle? High-level modules should not depend
on low-level modules. Both should depend on abstractions.
Abstractions should not depend upon details. Details should depend upon abstractions.
Don’t call us, we’ll call you
The principles
![Page 13: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/13.jpg)
Utility Layer
Mechanism Layer
Policy Layer
Dependency Inversion Principle
The principles
![Page 14: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/14.jpg)
Dependency Inversion Principle
The principles
Policy Layer
Mechanism Layer
Utility Layer
Policy Service Interface
Mechanism Service
Interface
![Page 15: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/15.jpg)
Dependency Inversion Principle
The principles
Policy Layer
Mechanism Layer
Utility Layer
Policy Service Interface
Mechanism Service
Interface
![Page 16: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/16.jpg)
Dependency Inversion Principle
Why should you use it? Reduces coupling Makes it easier to reuse classes Can make code easier to test Use IoC to help implement
The principles
![Page 17: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/17.jpg)
Dependency Inversion Principle
Code
The principles
![Page 18: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/18.jpg)
Open/closed Principle
What is the principle? Software entities (classes, modules,
functions, etc.) should be open for extension but closed for modification.
Add new code, don’t touch the old code Why should you use it?
Systems easier to change/evolve Reduces risk Develop and test extensions in isolation
The principles
![Page 19: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/19.jpg)
Open/closed Principle
Code
The principles
![Page 20: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/20.jpg)
Liskov Substitution Principle
What is the principle? If for each object o1 of type S there is an
object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.
Subtypes must be substitutable for their base types.
The principles
![Page 21: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/21.jpg)
Liskov Substitution Principle
Why should you use it? Consistent behaviour Reduces coupling Makes it easier to evolve code
The principles
![Page 22: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/22.jpg)
Liskov Substitution Principle
The principles
Fail
![Page 23: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/23.jpg)
Liskov Substitution Principle
The principles
Code
![Page 24: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/24.jpg)
The principles
S ingle Responsibility Principle –> SRP A class should have only one reason to change.
O pen/Closed Principle –>OCP Add new code, don’t touch the old code
L iskov Substitution Principle –> LSP Subtypes must be substitutable for their base types
I nterface Segregation Principle –> ISP Avoid “fat” interfaces
D ependency Inversion Principle –> DIP Don’t call us, we’ll call you
![Page 25: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/25.jpg)
S.O.L.I.D code is...
Loosely coupled Highly cohesive Easy to evolve/change Testable
![Page 26: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/26.jpg)
Summary
Not new, been around a while Principles not rules You decide when to apply It should make it easier to evolve
code Code will be easier to unit test
![Page 27: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/27.jpg)
Questions?
![Page 28: Is your code solid](https://reader036.fdocuments.us/reader036/viewer/2022062513/554f6683b4c905c8088b4dd9/html5/thumbnails/28.jpg)
Resources
Principles Of Ood DimeCasts.Net – screen casts on each
principle Los Techies – series of posts on
principles Hanselminutes – podcast with Uncle Bob Getting a good SOLID start – post by
Uncle bob Presentation Code – code from the
presentation@NathanGloyn
nathangloyn
Design Code [email protected]