+ All Categories
Home > Technology > Is your code solid

Is your code solid

Date post: 10-May-2015
Category:
Upload: nathan-gloyn
View: 2,513 times
Download: 4 times
Share this document with a friend
Description:
Slides from my talk at DDD SW4
Popular Tags:
28
IS YOUR CODE SOLID? Principles behind good software design @NathanGloyn nathangloy n Design Code Relea se [email protected]
Transcript
Page 2: Is your code solid

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

The origin of S.O.L.I.D

Who is this man?

Page 4: Is your code solid

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

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

Design Smells

Rigidity Fragility Immobility Viscosity Needless Complexity Needless Repetition Opacity

Page 7: Is your code solid

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

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

Single Responsibility Principle

Code

The principles

Page 10: Is your code solid

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

Interface Segregation Principle

Code

The principles

Page 12: Is your code solid

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

Utility Layer

Mechanism Layer

Policy Layer

Dependency Inversion Principle

The principles

Page 14: Is your code solid

Dependency Inversion Principle

The principles

Policy Layer

Mechanism Layer

Utility Layer

Policy Service Interface

Mechanism Service

Interface

Page 15: Is your code solid

Dependency Inversion Principle

The principles

Policy Layer

Mechanism Layer

Utility Layer

Policy Service Interface

Mechanism Service

Interface

Page 16: Is your code solid

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

Dependency Inversion Principle

Code

The principles

Page 18: Is your code solid

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

Open/closed Principle

Code

The principles

Page 20: Is your code solid

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

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

Liskov Substitution Principle

The principles

Fail

Page 23: Is your code solid

Liskov Substitution Principle

The principles

Code

Page 24: Is your code solid

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

S.O.L.I.D code is...

Loosely coupled Highly cohesive Easy to evolve/change Testable

Page 26: Is your code solid

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

Questions?


Recommended