Slide 1

Slide 1 text

FROM FROM STUPID STUPID TO TO SOLID SOLID CODE! CODE! A few basic principles of Object-Oriented Programming and Design.

Slide 2

Slide 2 text

DISCLAIMER DISCLAIMER THESE ARE PRINCIPLES, NOT LAWS! THESE ARE PRINCIPLES, NOT LAWS!

Slide 3

Slide 3 text

STUPID STUPID CODE, SERIOUSLY? CODE, SERIOUSLY?

Slide 4

Slide 4 text

WHAT MAKES CODE WHAT MAKES CODE STUPID STUPID? ? Singleton Tight Coupling Untestability Premature Optimization Indescriptive Naming Duplication

Slide 5

Slide 5 text

S SINGLETON INGLETON Programs using global state are very difficult to test. Programs that rely on global state hide their dependencies. Why Singletons Are Controversial Why is Singleton considered an anti pattern? So Singletons are bad, then what?

Slide 6

Slide 6 text

T TIGHT COUPLING IGHT COUPLING Generalization of the Singleton issue. Also known as strong coupling. Reducing Coupling (Martin Fowler)

Slide 7

Slide 7 text

U UNTESTABILITY NTESTABILITY Testing should not be hard! Whenever you don't write unit tests because you don't have time, the real issue is that your code is bad.

Slide 8

Slide 8 text

P PREMATURE OPTIMIZATION REMATURE OPTIMIZATION Premature optimization is the root of all evil. — Donald Knuth There is only cost and no benefit. PrematureOptimization Anti-Pattern

Slide 9

Slide 9 text

IINDESCRIPTIVE NAMING NDESCRIPTIVE NAMING Name your classes, methods, attributes, and variables properly. Don't abbreviate, never!

Slide 10

Slide 10 text

D DUPLICATION UPLICATION Don't Repeat Yourself! Keep It Simple, Stupid!

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

SOLID SOLID TO THE RESCUE! TO THE RESCUE!

Slide 13

Slide 13 text

SOLID SOLID A term describing a collection of design principles for good code that was coined by Robert C. Martin also known as Uncle Bob.

Slide 14

Slide 14 text

S SOLID OLID

Slide 15

Slide 15 text

S SINGLE RESPONSIBILITY PRINCIPLE (SRP) INGLE RESPONSIBILITY PRINCIPLE (SRP) There should never be more than one reason for a class to change. The Single Responsibility Principle

Slide 16

Slide 16 text

Just because you can, doesn't mean you should!

Slide 17

Slide 17 text

#PROTIPS #PROTIPS Split big classes Use layers Avoid god classes Write straightforward comments

Slide 18

Slide 18 text

S SO OLID LID

Slide 19

Slide 19 text

O OPEN/CLOSED PRINCIPLE (OCP) PEN/CLOSED PRINCIPLE (OCP) Software entities should be open for extension, but closed for modification. The Open-Closed Principle

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

#PROTIPS #PROTIPS Make all member variables private No global variables, ever Avoid setters (as much as possible)

Slide 22

Slide 22 text

SO SOL LID ID

Slide 23

Slide 23 text

L LISKOV SUBSTITUTION PRINCIPLE (LSP) ISKOV SUBSTITUTION PRINCIPLE (LSP) Objects in a program should be replaceable with instances of their subtypes without altering the correctness of the program. Liskov Substitution Principle The Liskov Substitution Principle

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

SOL SOLIID D

Slide 26

Slide 26 text

IINTERFACE SEGREGATION PRINCIPLE (ISP) NTERFACE SEGREGATION PRINCIPLE (ISP) Many client-specific interfaces are better than one general-purpose interface. The Interface Segregation Principle

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

SOLI SOLID D

Slide 29

Slide 29 text

D DEPENDENCY INVERSION PRINCIPLE (DIP) EPENDENCY INVERSION PRINCIPLE (DIP) High level modules should not depend upon low level modules. Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions. DIP in the Wild Dependency Inversion Principle The Dependency Inversion Principle Dependency Injection Is NOT The Same As The Dependency Inversion Principle

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

CONCLUSION CONCLUSION Avoiding tight coupling is the key! Use your brain. Writing SOLID code is not that hard.

Slide 32

Slide 32 text

THANK YOU, QUESTIONS? THANK YOU, QUESTIONS? williamdurand.fr github.com/willdurand twitter.com/couac

Slide 33

Slide 33 text

CREDITS CREDITS http://lostechies.com/derickbailey/2009/02/11/solid- development-principles-in-motivational-pictures/