Writing S.O.L.I.D Code

101
WRITING S.O.L.I.D CODE RA JEEV N B

Transcript of Writing S.O.L.I.D Code

Page 1: Writing S.O.L.I.D Code

WRITING S.O.L.I.D CODERAJEEV N B

Page 2: Writing S.O.L.I.D Code

RAJEEV N B

2

Developer at ThoughtWorks

Blogs at www.rajeevnb.com

@rbharshetty - Twitter

@rshetty - Github

Page 3: Writing S.O.L.I.D Code

XP

3

Page 4: Writing S.O.L.I.D Code

This is me doing XP

4

Page 5: Writing S.O.L.I.D Code

IN THE BEGINNING

5

YOUR APPLICATION WAS PERFECT

Page 6: Writing S.O.L.I.D Code

THEN SOMETHING HAPPENED

6

Page 7: Writing S.O.L.I.D Code

YOUR APPLICATION CHANGED

7

Page 8: Writing S.O.L.I.D Code

DESIGN TO THE RESCUE

8

Page 9: Writing S.O.L.I.D Code

DESIGN ?

9

Page 10: Writing S.O.L.I.D Code

It is all about managing your DEPENDENCIES

10

Page 11: Writing S.O.L.I.D Code

DEPENDENCIES

11

Client Dependent on Logger

Page 12: Writing S.O.L.I.D Code

DEPENDENCIES

12

Page 13: Writing S.O.L.I.D Code

DEPENDENCY MANAGEMENT

• Controlling interdependencies among various software

entities

• As interdependencies increase, Reusability, Flexibility

and Maintainability of your applications decrease

• Unmanaged dependencies lead to “Dependency Hell”

13

Page 14: Writing S.O.L.I.D Code

DEPENDENCY HELL

14

Page 15: Writing S.O.L.I.D Code

SUCCESSFUL DESIGN

• INCREASED COHESION

• DECREASED COUPLING

• SUPPORTS REUSE

• TESTABLE

• FLEXIBLE

15

Page 16: Writing S.O.L.I.D Code

DESIGN STAMINA

16

Page 17: Writing S.O.L.I.D Code

SOFTWARE ROTS

17

Page 18: Writing S.O.L.I.D Code

WHY DOES SOFTWARE ROT ?

• Changing requirements

• Improper Design

• No Continuos Design - Refactoring

• Limited Project Resources

• Complexity

18

Page 19: Writing S.O.L.I.D Code

ROBERT MARTIN

19

Design Principles and Design Patterns

Page 20: Writing S.O.L.I.D Code

SYMPTOMS OF ROTTING SOFTWARE

• RIGIDITY

• FRAGILITY

• IMMOBILITY

• VISCOSITY

20

Page 21: Writing S.O.L.I.D Code

RIGIDITY

21

Page 22: Writing S.O.L.I.D Code

22

Page 23: Writing S.O.L.I.D Code

RIGIDITY

• Difficult to change

• Cascading of Changes in Dependent Modules

• Impact of change cannot be predicted

• Thus, It can’t be estimated

• Time and Cost can’t be quantified

23

Page 24: Writing S.O.L.I.D Code

FRAGILITY

24

Page 25: Writing S.O.L.I.D Code

25

Page 26: Writing S.O.L.I.D Code

FRAGILITY

• Break many places when changed

• Errors appear in areas unrelated to changed areas

• Quality is unpredictable

• Development Team Credibility Loss

• Sometimes breakage increases with time

26

Page 27: Writing S.O.L.I.D Code

IMMOBILITY

27

Page 28: Writing S.O.L.I.D Code

28

Page 29: Writing S.O.L.I.D Code

IMMOBILITY

• Inability to reuse Software Components

• Lack of reusable modules

• Desirable parts of the system dependent on

undesirable parts

• Work and risk of extracting greater than cost of

writing it from scratch

29

Page 30: Writing S.O.L.I.D Code

VISCOSITY

30

Page 31: Writing S.O.L.I.D Code

31

Page 32: Writing S.O.L.I.D Code

VISCOSITY

• VISCOSITY OF DESIGN

• VISCOSITY OF ENVIRONMENT

32

Page 33: Writing S.O.L.I.D Code

VISCOSITY OF DESIGN

• Design Preserving methods are harder than Hacks

• Easy to do Wrong thing but harder to do right thing

• As time progresses, it will become increasingly difficult to

continue developing the application

33

Page 34: Writing S.O.L.I.D Code

VISCOSITY OF ENVIRONMENT

• Development Environment is slow and inefficient

• Slower feedback

Ex: Longer compile times, Slower builds, Long running tests

34

Page 35: Writing S.O.L.I.D Code

PREVENTING SOFTWARE ROT

• Refactoring (Continuos Improvement of Design)

• Testing (Unit/Integration/Contract tests)

• Code Reviews

• Documentation

• Design Principles

• Simplicity

35

Page 36: Writing S.O.L.I.D Code

OO DESIGN PRINCIPLES

36

Page 37: Writing S.O.L.I.D Code

S.O.L.I.D

37

Single Responsibility Principle

Open/Closed Principle

Liskov Substitution Principle

Interface Segregation Principle

Dependency Inversion Principle

Page 38: Writing S.O.L.I.D Code

SINGLE RESPONSIBILITY

38

Page 39: Writing S.O.L.I.D Code

39

Page 40: Writing S.O.L.I.D Code

40

A software entity should have one and only one

reason to change

Page 41: Writing S.O.L.I.D Code

BEFORE

41

Page 42: Writing S.O.L.I.D Code

AFTER

42

Page 43: Writing S.O.L.I.D Code

Why is it important to separate these two

responsibilities into separate classes ?

• Each responsibility is an axis of change

• Changes in one responsibility may affect another

• Coupling among classes leads to Fragile Design

43

Page 44: Writing S.O.L.I.D Code

SIGNS ?

44

• Description of class ( and/or ) • Rigidity and Fragility in code

Page 45: Writing S.O.L.I.D Code

BENEFITS ?

45

• Reduced Coupling • Separation of Concerns • Better readability • Your code evolves cleanly (Maintainable)

Page 46: Writing S.O.L.I.D Code

OPEN/CLOSED

46

Page 47: Writing S.O.L.I.D Code

47

Page 48: Writing S.O.L.I.D Code

48

Software entities should be closed for modification but

open for extension

Page 49: Writing S.O.L.I.D Code

OCP

49

Add functionality by adding new code, not

by modifying old code

Page 50: Writing S.O.L.I.D Code

ABSTRACTION IS THE KEY

50

• Client/Server relationship is open • Changes in Server cause changes in the

client

Page 51: Writing S.O.L.I.D Code

ABSTRACTION IS THE KEY

51

• Client/Server relationship is closed • Abstract Servers close clients to changes in

implementation

Page 52: Writing S.O.L.I.D Code

BEFORE

52

Page 53: Writing S.O.L.I.D Code

AFTER

53

Page 54: Writing S.O.L.I.D Code

SIGNS ?

54

● Switch cases littered all over your application

● Hunt and replace policy

Page 55: Writing S.O.L.I.D Code

BENEFITS ?

55

• Reusability

• Maintainability

• Flexible design

Page 56: Writing S.O.L.I.D Code

DESIGN BY CONTRACT

56

• Methods of class define pre-conditions and

post-conditions

• The preconditions must be met for method

to execute

• Upon completion, method guarantees that

the post conditions are met

Page 57: Writing S.O.L.I.D Code

DESIGN BY CONTRACT

57

Page 58: Writing S.O.L.I.D Code

LISKOV SUBSTITUTION

58

Page 59: Writing S.O.L.I.D Code

59

Sub-types must be substitutable for their

base- types

Page 60: Writing S.O.L.I.D Code

When is something Substitutable ?

60

Page 61: Writing S.O.L.I.D Code

Requires No Less Promises No More

61

Page 62: Writing S.O.L.I.D Code

ELLIPSE/CIRCLE

62

Page 63: Writing S.O.L.I.D Code

ELLIPSE/CIRCLE

63

● Violation of LSP

● Circle violates the post conditions

● Circle not substitutable for Ellipse

Page 64: Writing S.O.L.I.D Code

BEFORE

64

Page 65: Writing S.O.L.I.D Code

AFTER

65

Page 66: Writing S.O.L.I.D Code

SIGNS ?

66

• Explicit Checking type of a class in the code

• Derived types not substitutable for the base

types

Page 67: Writing S.O.L.I.D Code

BENEFITS ?

67

• Ability to re-use code increases • Flexible code

Page 68: Writing S.O.L.I.D Code

INTERFACE SEGREGATION

68

Page 69: Writing S.O.L.I.D Code

69

Page 70: Writing S.O.L.I.D Code

70

Clients should not be forced to depend on methods it

does not use

Page 71: Writing S.O.L.I.D Code

BEFORE

71

Page 72: Writing S.O.L.I.D Code

AFTER

72

Page 73: Writing S.O.L.I.D Code

SIGNS ?

73

● Client requiring things it does not need

● Methods littered with “NotImplemented”

exceptions

Page 74: Writing S.O.L.I.D Code

BENEFITS ?

74

• Lot of small, focused interfaces • Flexible design

Page 75: Writing S.O.L.I.D Code

DEPENDENCY INVERSION

75

Page 76: Writing S.O.L.I.D Code

76

Page 77: Writing S.O.L.I.D Code

77

Depend upon Abstractions not upon Concretions

Page 78: Writing S.O.L.I.D Code

DIP

78

• High Level Modules should not depend on low level modules, Both should depend on Abstractions

• Abstractions should not depend on details. Details should depend on abstractions.

Page 79: Writing S.O.L.I.D Code

BEFORE

79

Page 80: Writing S.O.L.I.D Code

AFTER

80

Page 81: Writing S.O.L.I.D Code

BEFORE

81

Page 82: Writing S.O.L.I.D Code

AFTER

82

Page 83: Writing S.O.L.I.D Code

SIGNS ?

83

● High level modules are harder to reuse

● Changes in the lower level modules directly

affect the higher level modules

Page 84: Writing S.O.L.I.D Code

ADVANTAGES ?

84

• Reusable modules • Easier to extend • Low Coupled code

Page 85: Writing S.O.L.I.D Code

KENT BECK

85

4 Simple Design Rules

Page 86: Writing S.O.L.I.D Code

PASSES TESTS

86

Page 87: Writing S.O.L.I.D Code

87

Page 88: Writing S.O.L.I.D Code

PASSES TESTS

88

● Unit tests with Red, Green, Refactor cycles

● Tests help communicate Design

Page 89: Writing S.O.L.I.D Code

REVEALS INTENTION

89

Page 90: Writing S.O.L.I.D Code

90

Page 91: Writing S.O.L.I.D Code

REVEALS INTENTION

91

● Self Documenting code

● Meaningful names to software entities

● Clear code

Page 92: Writing S.O.L.I.D Code

NO DUPLICATION

92

Page 93: Writing S.O.L.I.D Code

93

Page 94: Writing S.O.L.I.D Code

NO DUPLICATION

94

● Once and only once

● DRY

Page 95: Writing S.O.L.I.D Code

FEWEST ELEMENTS

95

Page 96: Writing S.O.L.I.D Code

96

Page 97: Writing S.O.L.I.D Code

FEWEST ELEMENTS

97

● No Superfluous parts/elements

● YAGNI

● KISS

● Minimum number of classes/methods

Page 98: Writing S.O.L.I.D Code

98

Page 99: Writing S.O.L.I.D Code

WRAPPING UP

99

● SRP - One responsibility per class

● OCP - Extend not modify

● LSP - Derived substitutable for base

● ISP - Split into Focussed interfaces

● DIP - Depend on Abstractions

Page 100: Writing S.O.L.I.D Code

REFERENCES

100

● Design Principles and Patterns (http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf)

● Design By Contract - Eiffel (https://www.eiffel.com/values/design-by-contract/)

● Code from the talk - Java: https://github.com/rShetty/S.O.L.I.D-Java

● Code from the talk - Ruby: https://github.com/rshetty/S.O.L.I.D

Page 101: Writing S.O.L.I.D Code

THANK YOU