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

S.O.L.I.D xp

Apr 16, 2017

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: S.O.L.I.D xp

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

Page 2: S.O.L.I.D xp

RAJEEV N B

2

Developer at ThoughtWorks

Blogs at www.rajeevnb.com

@rbharshetty - Twitter

@rshetty - Github

Page 3: S.O.L.I.D xp

XP

3

Page 4: S.O.L.I.D xp

This is me doing XP

4

Page 5: S.O.L.I.D xp

IN THE BEGINNING

5

YOUR APPLICATION WAS PERFECT

Page 6: S.O.L.I.D xp

THEN SOMETHING HAPPENED

6

Page 7: S.O.L.I.D xp

YOUR APPLICATION CHANGED

7

Page 8: S.O.L.I.D xp

DESIGN TO THE RESCUE

8

Page 9: S.O.L.I.D xp

DESIGN ?

9

Page 10: S.O.L.I.D xp

It is all about managing your DEPENDENCIES

10

Page 11: S.O.L.I.D xp

DEPENDENCIES

11

Client Dependent on Logger

Page 12: S.O.L.I.D xp

DEPENDENCIES

12

Page 13: S.O.L.I.D xp

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: S.O.L.I.D xp

DEPENDENCY HELL

14

Page 15: S.O.L.I.D xp

SUCCESSFUL DESIGN

• INCREASED COHESION

• DECREASED COUPLING

• SUPPORTS REUSE

• TESTABLE

• FLEXIBLE

15

Page 16: S.O.L.I.D xp

DESIGN STAMINA

16

Page 17: S.O.L.I.D xp

SOFTWARE ROTS

17

Page 18: S.O.L.I.D xp

WHY DOES SOFTWARE ROT ?

• Changing requirements

• Improper Design

• No Continuos Design - Refactoring

• Limited Project Resources

• Complexity

18

Page 19: S.O.L.I.D xp

ROBERT MARTIN

19

Design Principles and Design Patterns

Page 20: S.O.L.I.D xp

SYMPTOMS OF ROTTING SOFTWARE

• RIGIDITY

• FRAGILITY

• IMMOBILITY

• VISCOSITY

20

Page 21: S.O.L.I.D xp

RIGIDITY

21

Page 22: S.O.L.I.D xp

22

Page 23: S.O.L.I.D xp

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: S.O.L.I.D xp

FRAGILITY

24

Page 25: S.O.L.I.D xp

25

Page 26: S.O.L.I.D xp

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: S.O.L.I.D xp

IMMOBILITY

27

Page 28: S.O.L.I.D xp

28

Page 29: S.O.L.I.D xp

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: S.O.L.I.D xp

VISCOSITY

30

Page 31: S.O.L.I.D xp

31

Page 32: S.O.L.I.D xp

VISCOSITY

• VISCOSITY OF DESIGN

• VISCOSITY OF ENVIRONMENT

32

Page 33: S.O.L.I.D xp

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: S.O.L.I.D xp

VISCOSITY OF ENVIRONMENT

• Development Environment is slow and inefficient

• Slower feedback

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

34

Page 35: S.O.L.I.D xp

PREVENTING SOFTWARE ROT

• Refactoring (Continuos Improvement of Design)

• Testing (Unit/Integration/Contract tests)

• Code Reviews

• Documentation

• Design Principles

• Simplicity

35

Page 36: S.O.L.I.D xp

OO DESIGN PRINCIPLES

36

Page 37: S.O.L.I.D xp

S.O.L.I.D

37

Single Responsibility Principle

Open/Closed Principle

Liskov Substitution Principle

Interface Segregation Principle

Dependency Inversion Principle

Page 38: S.O.L.I.D xp

SINGLE RESPONSIBILITY

38

Page 39: S.O.L.I.D xp

39

Page 40: S.O.L.I.D xp

40

A software entity should have one and only one

reason to change

Page 41: S.O.L.I.D xp

BEFORE

41

Page 42: S.O.L.I.D xp

AFTER

42

Page 43: S.O.L.I.D xp

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: S.O.L.I.D xp

SIGNS ?

44

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

Page 45: S.O.L.I.D xp

BENEFITS ?

45

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

Page 46: S.O.L.I.D xp

OPEN/CLOSED

46

Page 47: S.O.L.I.D xp

47

Page 48: S.O.L.I.D xp

48

Software entities should be closed for modification but

open for extension

Page 49: S.O.L.I.D xp

OCP

49

Add functionality by adding new code, not

by modifying old code

Page 50: S.O.L.I.D xp

ABSTRACTION IS THE KEY

50

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

client

Page 51: S.O.L.I.D xp

ABSTRACTION IS THE KEY

51

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

implementation

Page 52: S.O.L.I.D xp

BEFORE

52

Page 53: S.O.L.I.D xp

AFTER

53

Page 54: S.O.L.I.D xp

SIGNS ?

54

● Switch cases littered all over your application

● Hunt and replace policy

Page 55: S.O.L.I.D xp

BENEFITS ?

55

• Reusability

• Maintainability

• Flexible design

Page 56: S.O.L.I.D xp

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: S.O.L.I.D xp

DESIGN BY CONTRACT

57

Page 58: S.O.L.I.D xp

LISKOV SUBSTITUTION

58

Page 59: S.O.L.I.D xp

59

Sub-types must be substitutable for their

base- types

Page 60: S.O.L.I.D xp

When is something Substitutable ?

60

Page 61: S.O.L.I.D xp

Requires No Less Promises No More

61

Page 62: S.O.L.I.D xp

ELLIPSE/CIRCLE

62

Page 63: S.O.L.I.D xp

ELLIPSE/CIRCLE

63

● Violation of LSP

● Circle violates the post conditions

● Circle not substitutable for Ellipse

Page 64: S.O.L.I.D xp

BEFORE

64

Page 65: S.O.L.I.D xp

AFTER

65

Page 66: S.O.L.I.D xp

SIGNS ?

66

• Explicit Checking type of a class in the code

• Derived types not substitutable for the base

types

Page 67: S.O.L.I.D xp

BENEFITS ?

67

• Ability to re-use code increases • Flexible code

Page 68: S.O.L.I.D xp

INTERFACE SEGREGATION

68

Page 69: S.O.L.I.D xp

69

Page 70: S.O.L.I.D xp

70

Clients should not be forced to depend on methods it

does not use

Page 71: S.O.L.I.D xp

BEFORE

71

Page 72: S.O.L.I.D xp

AFTER

72

Page 73: S.O.L.I.D xp

SIGNS ?

73

● Client requiring things it does not need

● Methods littered with “NotImplemented”

exceptions

Page 74: S.O.L.I.D xp

BENEFITS ?

74

• Lot of small, focused interfaces • Flexible design

Page 75: S.O.L.I.D xp

DEPENDENCY INVERSION

75

Page 76: S.O.L.I.D xp

76

Page 77: S.O.L.I.D xp

77

Depend upon Abstractions not upon Concretions

Page 78: S.O.L.I.D xp

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: S.O.L.I.D xp

BEFORE

79

Page 80: S.O.L.I.D xp

AFTER

80

Page 81: S.O.L.I.D xp

BEFORE

81

Page 82: S.O.L.I.D xp

AFTER

82

Page 83: S.O.L.I.D xp

SIGNS ?

83

● High level modules are harder to reuse

● Changes in the lower level modules directly

affect the higher level modules

Page 84: S.O.L.I.D xp

ADVANTAGES ?

84

• Reusable modules • Easier to extend • Low Coupled code

Page 85: S.O.L.I.D xp

KENT BECK

85

4 Simple Design Rules

Page 86: S.O.L.I.D xp

PASSES TESTS

86

Page 87: S.O.L.I.D xp

87

Page 88: S.O.L.I.D xp

PASSES TESTS

88

● Unit tests with Red, Green, Refactor cycles

● Tests help communicate Design

Page 89: S.O.L.I.D xp

REVEALS INTENTION

89

Page 90: S.O.L.I.D xp

90

Page 91: S.O.L.I.D xp

REVEALS INTENTION

91

● Self Documenting code

● Meaningful names to software entities

● Clear code

Page 92: S.O.L.I.D xp

NO DUPLICATION

92

Page 93: S.O.L.I.D xp

93

Page 94: S.O.L.I.D xp

NO DUPLICATION

94

● Once and only once

● DRY

Page 95: S.O.L.I.D xp

FEWEST ELEMENTS

95

Page 96: S.O.L.I.D xp

96

Page 97: S.O.L.I.D xp

FEWEST ELEMENTS

97

● No Superfluous parts/elements

● YAGNI

● KISS

● Minimum number of classes/methods

Page 98: S.O.L.I.D xp

98

Page 99: S.O.L.I.D xp

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: S.O.L.I.D xp

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: S.O.L.I.D xp

THANK YOU