Slide 1

Slide 1 text

CLEAN CODE IN PRACTICE / Antoine Vernois @avernois

Slide 2

Slide 2 text

a.b.c.d THANKS ITAKE ORGANIZERS JÉRÔME AVOUSTIN YOU !

Slide 3

Slide 3 text

ANTOINE VERNOIS Journeyman @avernois

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

DISCLAIMER Object Oriented Language I won't go into details Some points will look stupidly simple I will repeat myself

Slide 7

Slide 7 text

AGILE & QUALITY Continuous attention to technical excellence and good design enhances agility. Agile Manifesto, 9th principle

Slide 8

Slide 8 text

WHY QUALITY ?

Slide 9

Slide 9 text

AGILE & QUALITY (AGAIN) Responding to change over following a plan. Agile Manifesto, 4th value

Slide 10

Slide 10 text

WHAT IS QUALITY ? A software is of good quality if the cost to add a feature does not change over time. Jérôme Avoustin

Slide 11

Slide 11 text

WHAT IS A CLEAN CODE ? Clean code is not about beautifullness, it's about goodness. Rebecca Wirfs-Brock tested ! no duplication reveals its intention easy to read B. Stroustrup, R. Jeffries, K. Beck, M. Feathers, W. Cunningham, ...

Slide 12

Slide 12 text

ABOUT TESTS When I hear tests are for those who don't know how to code

Slide 13

Slide 13 text

SMELLS

Slide 14

Slide 14 text

SMELLS Singleton Tight coupling Untestable Premature Optimisation Indescriptive naming Duplication

Slide 15

Slide 15 text

GOOD PRACTICES

Slide 16

Slide 16 text

GOOD PRACTICES Single Responsability Open-closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle

Slide 17

Slide 17 text

GOOD NAMING names should reveal the intent alt+shift+r is your friend (in eclipse) ... LET'S SEE FOR REAL.

Slide 18

Slide 18 text

DUPLICATION DON'T REPEAT YOURSELF !

Slide 19

Slide 19 text

ABSTRACTION 1 LEVEL BY METHOD

Slide 20

Slide 20 text

ABSTRACTION DEMETER'S LAW DON'T TALK TO STRANGER a . g e t B ( ) . g e t C ( ) . d o T h i n g s ( ) ;

Slide 21

Slide 21 text

TIGHT COUPLING IT'S BAD ! Tests Reuse ...

Slide 22

Slide 22 text

DEPENDANCY INJECTION p u b l i c c l a s s A { p r i v a t e B b ; p u b l i c v o i d e x e c u t e ( i n t i ) { b = n e w B ( ) ; C c = n e w C ( ) ; i f ( i < = 3 ) { b . e x e c u t e B ( ) ; } e l s e { c . e x e c u t e C ( ) ; } } }

Slide 23

Slide 23 text

METHODS SHORT

Slide 24

Slide 24 text

COMMENTS Comments are always failure Uncle Bob Don't comment bad code. Rewrite it. Brian W. Kernighan, P.J. Plaugher

Slide 25

Slide 25 text

COMMENTS FROM REAL LIFE / * * A c o m m e n t t o p l e a s e c h e c k s t y l e * / / * * S e t t h e p o r t * * @ p a r a m s p o r t * / p u b l i c v o i d s e t P o r t ( P o r t p o r t ) { t h i s . p o r t = p o r t } . . . } / * e n d f o r * / d a o . f l u s h ( ) ; d e f a u l t : b r e a k ; } / * e n d s w i t c h * / } / * e n d i f * / } / * e n d i f * / } c a t c h . . .

Slide 26

Slide 26 text

COMMENTS : THE EXCEPTION EXPLAIN THE WHY !

Slide 27

Slide 27 text

COMMENTS TEST TELLS ME WHAT CODE TELLS ME HOW COMMENT, IF NEEDED, TELLS ME WHY

Slide 28

Slide 28 text

HANDLING EXCEPTION FAIL FAST 1. Do not check exception 2. Manage exception as high as possible 3. Never ever return null !

Slide 29

Slide 29 text

TESTS The give you confidence to change the code. one test, one assert readibility tests document your code A TEST SHOULD TELL YOU A STORY TESTS ARE FIRST CLASS CITIZEN !

Slide 30

Slide 30 text

SOME MORE ADVICES Don't think inheritance, think polymorphism Don't think if/switch, think polymorphism AVOID SINGLETONS Did i warn you i will repeat myself ?

Slide 31

Slide 31 text

READ LIST

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

QUOTES Any fool can write code that a computer can understand. Good programmers write code that human can understand. Martin Fowler Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. Martin Golding ...if you're afraid to change something it is clearly poorly designed. Martin Fowler

Slide 34

Slide 34 text

AND ALL OF THIS, IT STARTS NEXT TIME YOU'LL READ OR WRITE CODE

Slide 35

Slide 35 text

THANK YOU ! ANY QUESTION ?