62
Design Principles in a Nutshell Dmytro Panin & Anton Udovychenko 12.2015

Design principles in a nutshell

Embed Size (px)

Citation preview

Page 1: Design principles in a nutshell

Design Principles in a NutshellDmytro Panin & Anton Udovychenko

12.2015

Page 2: Design principles in a nutshell

Agenda

▪ What is a good design?

▪ Complexity and simplicity

▪ KISS

▪ YAGNI

▪ Cohesiveness and coupling

▪ SOLID

▪ Q&A

Page 3: Design principles in a nutshell

Good design

▪ Design is good when a cost of changing of a design is minimum

Page 4: Design principles in a nutshell

Good design

▪ Design is good when a cost of changing of a design is minimum

▪ Easy to change

Page 5: Design principles in a nutshell

Good design

▪ Design is good when a cost of changing of a design is minimum

▪ Easy to change

Almost impossible to get it right the first time

Page 6: Design principles in a nutshell

How to make a good design?

Page 7: Design principles in a nutshell

How to make a good design?

▪ Let go of the ego

Page 8: Design principles in a nutshell

How to make a good design?

▪ Let go of the ego

▪ Be unemotional

Page 9: Design principles in a nutshell

How to make a good design?

▪ Let go of the ego

▪ Be unemotional

▪ Do code reviews

Page 10: Design principles in a nutshell

How to make a good design?

▪ Let go of the ego

▪ Be unemotional

▪ Do code reviews

▪ A good design is the one that hides inherited complexity and eliminates the accidental complexity

Page 11: Design principles in a nutshell

How to make a good design?

▪ Let go of the ego

▪ Be unemotional

▪ Do code reviews

▪ A good design is the one that hides inherited complexity and eliminates the accidental complexity

Software is never written, it is always rewritten. As the software that is relevant always must be changed

Page 12: Design principles in a nutshell

Complexity

▪ Inherited from a domain

▪ Accidental complexity

Page 13: Design principles in a nutshell

KISS - Keep it simple and stupid

Page 14: Design principles in a nutshell

What is simple?

Page 15: Design principles in a nutshell

What is simple?

▪ Simple makes you concentrated

Page 16: Design principles in a nutshell

What is simple?

▪ Simple makes you concentrated

▪ Simple resolves only actual issues we know about

Page 17: Design principles in a nutshell

What is simple?

▪ Simple makes you concentrated

▪ Simple resolves only actual issues we know about

▪ Simple fails less

Page 18: Design principles in a nutshell

What is simple?

▪ Simple makes you concentrated

▪ Simple resolves only actual issues we know about

▪ Simple fails less

▪ Simple is easier to understand

Page 19: Design principles in a nutshell

Simple is not necessarily familiar.

Page 20: Design principles in a nutshell

Think YAGNI

Do we need it?

Day 1

We have to ask ourselves whether we need it today or not. If it's possible to postpone something constantly for a few iterations maybe you don't need it at all.

Page 21: Design principles in a nutshell

Think YAGNI

Do we need it?

Day 1

Do we actually need it?

Day 5

We have to ask ourselves whether we need it today or not. If it's possible to postpone something constantly for a few iterations maybe you don't need it at all.

Page 22: Design principles in a nutshell

Think YAGNI

Do we need it?

Day 1

Do we actually need it?

Day 5

We do not need it

Day 9

We have to ask ourselves whether we need it today or not. If it's possible to postpone something constantly for a few iterations maybe you don't need it at all.

Page 23: Design principles in a nutshell

Cost of implementing

Smart Smarter Much smarter

Now correlation Later Result

X > Y Postpone

X == Y Postpone

X < Y ?

Page 24: Design principles in a nutshell

Do not confuse postponement and procrastination

Page 25: Design principles in a nutshell

Coupling

▪ Coupling is what you depend on...

Page 26: Design principles in a nutshell

Coupling

▪ Coupling is what you depend on...

▪ Inheritance is the worst form of coupling

Page 27: Design principles in a nutshell

Coupling

▪ Coupling is what you depend on...

▪ Inheritance is the worst form of coupling

▪ "knock out before mock out“

Page 28: Design principles in a nutshell

Coupling

▪ Coupling is what you depend on....

▪ Inheritance is the worst form of coupling

▪ "knock out before mock out“

▪ Depending on a class is a tight coupling

Page 29: Design principles in a nutshell

Coupling

▪ Coupling is what you depend on....

▪ Inheritance is the worst form of coupling

▪ "knock out before mock out“

▪ Depending on a class is a tight coupling

▪ Depending on an interface is a loose coupling

Page 30: Design principles in a nutshell

Coupling

▪ Coupling is what you depend on....

▪ Inheritance is the worst form of coupling

▪ "knock out before mock out“

A good design has high cohesion and low coupling

▪ Depending on a class is a tight coupling

▪ Depending on an interface is a loose coupling

Page 31: Design principles in a nutshell

DRY ( Do not Repeat Yourself )

▪ Every piece of knowledge in a system should have a single unambiguous authoritative representation

▪ It reduces the cost of development

Page 32: Design principles in a nutshell

SOLID

▪ Single responsibility principle

▪ Open-Closed Principle

▪ Liskov substitution principle

▪ Interface segregation principle

▪ Dependency inversion principle

Page 33: Design principles in a nutshell

Single responsibility principle

Page 34: Design principles in a nutshell

Single responsibility principle

▪ A class should have only a single responsibility

Page 35: Design principles in a nutshell

Single responsibility principle

▪ A class should have only a single responsibility

▪ Cohesive class has single responsibility

Page 36: Design principles in a nutshell

Single responsibility principle

▪ A class should have only a single responsibility

▪ Cohesive class has single responsibility

▪ When you say "this class does this AND that" you have violated SRP

Page 37: Design principles in a nutshell

Single responsibility principle

▪ A class should have only a single responsibility

▪ Cohesive class has single responsibility

▪ When you say "this class does this AND that" you have violated SRP

▪ Long methods are bad

Page 38: Design principles in a nutshell

Commenting

Page 39: Design principles in a nutshell

Commenting

▪ Comments are usually used to cover bad code

Page 40: Design principles in a nutshell

Commenting

▪ Comments are usually used to cover bad code

▪ When code is not self-evident refactor it

Page 41: Design principles in a nutshell

Commenting

▪ Comments are usually used to cover bad code

▪ When code is not self-evident refactor it

▪ Don't comment what, instead comment why

Page 42: Design principles in a nutshell

Commenting

▪ Comments are usually used to cover bad code

▪ When code is not self-evident refactor it

▪ Don't comment what, instead comment why

▪ A good code is like a joke

Page 43: Design principles in a nutshell

Commenting

▪ Comments are usually used to cover bad code

▪ When code is not self-evident refactor it

▪ Don't comment what, instead comment why

▪ A good code is like a joke

▪ Compose method pattern

Page 44: Design principles in a nutshell

Open-Closed Principle

Page 45: Design principles in a nutshell

Open-Closed Principle

▪ Two options - to make an enhancement1. Change existing code

2. Add a small new module of code

Page 46: Design principles in a nutshell

Open-Closed Principle

▪ Two options - to make an enhancement1. Change existing code

2. Add a small new module of code

▪ Nobody likes existing code

Page 47: Design principles in a nutshell

Open-Closed Principle

▪ Two options - to make an enhancement1. Change existing code

2. Add a small new module of code

▪ Nobody likes existing code

▪ Software module should be open for extension but closed from modification

Page 48: Design principles in a nutshell

Open-Closed Principle

▪ Two options - to make an enhancement1. Change existing code

2. Add a small new module of code

▪ Nobody likes existing code

▪ Software module should be open for extension but closed from modification

▪ Rely on abstraction and polymorphism

Page 49: Design principles in a nutshell
Page 50: Design principles in a nutshell

Liskov substitution principle

Page 51: Design principles in a nutshell

Liskov substitution principle

▪ Inheritance is overused

Page 52: Design principles in a nutshell

Liskov substitution principle

▪ Inheritance is overused▪ If an object of B should be used anywhere an object of A is used then use inheritance

Page 53: Design principles in a nutshell

Liskov substitution principle

▪ Inheritance is overused▪ If an object of B should be used anywhere an object of A is used then use inheritance

▪ If an object of B should use an object of A, then use composition / delegation

Page 54: Design principles in a nutshell

Liskov substitution principle

▪ Inheritance is overused▪ If an object of B should be used anywhere an object of A is used then use inheritance

▪ If an object of B should use an object of A, then use composition / delegation

▪ Inheritance demands more from a developer than composition or delegation does

Page 55: Design principles in a nutshell

Liskov substitution principle

▪ Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program

▪ The user of a base class should be able to use an instance of a derived class without knowing the difference

Page 56: Design principles in a nutshell
Page 57: Design principles in a nutshell

Interface segregation principle

▪ Many client-specific interfaces are better than one general-purpose interface

Alarm

Clock PCPhone TV Rooster…

Clock vs

Page 58: Design principles in a nutshell

Dependency inversion principle

▪ High-level modules should not depend on low-level modules. Both should depend on abstractions

Page 59: Design principles in a nutshell

Dependency inversion 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

Page 60: Design principles in a nutshell
Page 61: Design principles in a nutshell

Dependency injection pattern▪ Dependency inversion ≠ dependency injection

▪ Dependency injection pattern is an implementation of Dependency inversion principle

Page 62: Design principles in a nutshell

Questions?

Thank you

Inspired by: Core Design Principles for Software Developers by Venkat Subramaniam