Top Banner
Clean Code Presentation of the Uncle Bob's Book. 1 Thursday, December 1, 11
46

Clean Code

Oct 29, 2014

Download

Technology

Brice Argenson

Slides des conférences publiques hébergées par SUPINFO (http://www.supinfo.com) données en Octobre 2011 à Nantes et Novembre 2011 à Montréal.
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: Clean Code

Clean Code

Presentation of the Uncle Bob's Book.

1Thursday, December 1, 11

Page 2: Clean Code

The Speaker

• Brice Argenson– Full Professor in Java and Web Technologies

• Professional Experiences :

• Certifications :

– Bug Out PC– ADF– Adullact– Webpulser

– Audaxis– Groupe Open– Atos Worldline– Xebia IT

– SCJP 6– SCWCD 5

– MCP – Exam 70-316

[email protected] @bargenson

2Thursday, December 1, 11

Page 3: Clean Code

Agenda

• Software Craftsmanship

• Why Clean Code ?

• Tips & Tricks to write Clean Code

• References

3Thursday, December 1, 11

Page 4: Clean Code

SOFTWARE CRAFTSMANSHIPWhat is that ?

4Thursday, December 1, 11

Page 5: Clean Code

5

5Thursday, December 1, 11

Page 6: Clean Code

6

6Thursday, December 1, 11

Page 7: Clean Code

WHY CLEAN CODE ?Bad Code VS. Clean Code

7Thursday, December 1, 11

Page 8: Clean Code

8Thursday, December 1, 11

Page 9: Clean Code

Bad Code

• Bad Code killed some companies– Rushed the product to market– More and more features– Worse and worse code– Code unmanageable

• We all wrote bad code– Trying to go fast ?– We were in a rush ?– We’ll clean it up later…

9Thursday, December 1, 11

Page 10: Clean Code

10Thursday, December 1, 11

Page 11: Clean Code

The Cost of Bad Code

• Do you have been slowed down by messy code ?

• More & more mess è Less & less productivity

• When productivity became terrible è Redesign

• And again…• And again…

11Thursday, December 1, 11

Page 12: Clean Code

Whose Fault ?

• The customer ?– The requirements change too much ?

• The managers ?– The deadlines are too short ?

• It is mainly the developer fault !– The others look to us for the information they need– The doctor knows more than the patient about the risks

[…]

• Be professionals !

12Thursday, December 1, 11

Page 13: Clean Code

What is Clean Code ?

• What is Clean Code for you ?– It is working ?– It is efficient ?– It is short ?

• Clean Code is easy to read !

• We are authors !

13Thursday, December 1, 11

Page 14: Clean Code

What is Clean Code ?

• The Boy Scout Rule :– Leave the campground cleaner than you found it.

• Refactor your code !

• Write tests !– Test Driven Development

14Thursday, December 1, 11

Page 15: Clean Code

GET YOUR HANDS DIRTY !How to write Clean Code ?

15Thursday, December 1, 11

Page 16: Clean Code

Meaningful Names

• What is the purpose of this code ?

16Thursday, December 1, 11

Page 17: Clean Code

Meaningful Names

• And what do you think of this code ?

17Thursday, December 1, 11

Page 18: Clean Code

Meaningful Names

• Use Intention-Revealing Names

18Thursday, December 1, 11

Page 19: Clean Code

Meaningful Names

• What about Hungarian Notation ?

19Thursday, December 1, 11

Page 20: Clean Code

Meaningful Names

• What if one day we change the type ?

• Hungarian Notation make it harder to change the name or type of a variable, function or class

20Thursday, December 1, 11

Page 21: Clean Code

Meaningful Names

• What about Member Prefixes ?– Example: m_firstName

• Classes and functions should be small enough that you don’t need them

• IDE highlights or colorizes members !

• Prefixes become unseen clutter

21Thursday, December 1, 11

Page 23: Clean Code

Functions

• Small !– Not bigger than a screen full

• Correct in the eighties (24 x 80)

– Should be transparently obvious– Indent level should not be greater than one or two

• Do one thing !– Your function is divided into section ?

• You can divide it !

• The Stepdown Rule !– We want the code to read like a top-down narrative

23Thursday, December 1, 11

Page 24: Clean Code

Functions

• Function arguments ?– Niladic

• The ideal number

– Monadic• A good number

– Dyadic• A good number

– Triadic• Should be avoided

– Polyadic• Do you need to wrap the arguments into a new type ?

24Thursday, December 1, 11

Page 25: Clean Code

Error Handling

• What do you think about this code ?

25Thursday, December 1, 11

Page 26: Clean Code

Error Handling

• Prefer exceptions to returning error codes

26Thursday, December 1, 11

Page 27: Clean Code

Error Handling

• And what do you think about this code ?

27Thursday, December 1, 11

Page 28: Clean Code

Error Handling

• For you what is the most common exception ?– NPE !

• How to avoid it ?

28

28Thursday, December 1, 11

Page 29: Clean Code

Error Handling

• What do you think about this code ?

29

29Thursday, December 1, 11

Page 30: Clean Code

Error Handling

• Don’t return Null !

30

30Thursday, December 1, 11

Page 31: Clean Code

Error Handling

• What do you think about this code ?

31

31Thursday, December 1, 11

Page 32: Clean Code

Error Handling

• How to make code look like that ?

• Special Case object !

32

32Thursday, December 1, 11

Page 33: Clean Code

Side Effects

• Consider the following class :

33

33Thursday, December 1, 11

Page 34: Clean Code

Side Effects

• Consider the following code :

• Do you see the problem ?

34

34Thursday, December 1, 11

Page 35: Clean Code

Side Effects

• To protect the internals of a Period instance from this sort of attack:

– You must make defensive copy of each mutable parameter to the constructor !

35

35Thursday, December 1, 11

Page 36: Clean Code

Side Effects

• Inheritance is a powerful way to achieve code reuse– But not always the best !

• Inheritance from ordinary concrete classes across package boundaries is dangerous !

• Unlike method invocation, inheritance violates encapsulation

36

36Thursday, December 1, 11

Page 37: Clean Code

Side Effects

37

37Thursday, December 1, 11

Page 38: Clean Code

Side Effects

• What this code display ?

38

38Thursday, December 1, 11

Page 39: Clean Code

Side Effects

39

39Thursday, December 1, 11

Page 40: Clean Code

Side Effects

40

40Thursday, December 1, 11

Page 41: Clean Code

Side Effects

• Design of the InstrumentedSet is extremely flexible :– Implement the Set interface– Receive an argument also of type Set

• With inheritance, we could work only with HashSet

• With composition, we can work with any Set implementation !

41

41Thursday, December 1, 11

Page 42: Clean Code

Side Effects

• Favor composition over inheritance !

42

42Thursday, December 1, 11

Page 43: Clean Code

REFERENCESDo you want to read more about the subject ?

43

43Thursday, December 1, 11

Page 44: Clean Code

Books

44

Clean CodeA Handbook of Agile Software Craftsmanship

Robert C. Martin (aka. Uncle Bob)

Prentice Hall editions

44Thursday, December 1, 11

Page 45: Clean Code

Books

45

Effective JavaSecond Edition

Joshua Bloch

Addison Wesley editions

45Thursday, December 1, 11

Page 46: Clean Code

Links

• Sign the Manifesto – http://manifesto.softwarecraftsmanship.org

• Software Craftsmanship en pratique - Xebia– http://blog.xebia.fr/2011/01/31/software-craftsmanship-

en-pratique/

• The Clean Coder - Uncle Bob blog– http://thecleancoder.blogspot.com/

46

46Thursday, December 1, 11