clean code

Post on 29-Oct-2014

16 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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.

TRANSCRIPT

Clean Code

Presentation of the Uncle Bob's Book.

1Thursday, December 1, 11

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

brice.argenson@supinfo.com @bargenson

2Thursday, December 1, 11

Agenda

• Software Craftsmanship

• Why Clean Code ?

• Tips & Tricks to write Clean Code

• References

3Thursday, December 1, 11

SOFTWARE CRAFTSMANSHIPWhat is that ?

4Thursday, December 1, 11

5

5Thursday, December 1, 11

6

6Thursday, December 1, 11

WHY CLEAN CODE ?Bad Code VS. Clean Code

7Thursday, December 1, 11

8Thursday, December 1, 11

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

10Thursday, December 1, 11

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

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

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

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

GET YOUR HANDS DIRTY !How to write Clean Code ?

15Thursday, December 1, 11

Meaningful Names

• What is the purpose of this code ?

16Thursday, December 1, 11

Meaningful Names

• And what do you think of this code ?

17Thursday, December 1, 11

Meaningful Names

• Use Intention-Revealing Names

18Thursday, December 1, 11

Meaningful Names

• What about Hungarian Notation ?

19Thursday, December 1, 11

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

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

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

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

Error Handling

• What do you think about this code ?

25Thursday, December 1, 11

Error Handling

• Prefer exceptions to returning error codes

26Thursday, December 1, 11

Error Handling

• And what do you think about this code ?

27Thursday, December 1, 11

Error Handling

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

• How to avoid it ?

28

28Thursday, December 1, 11

Error Handling

• What do you think about this code ?

29

29Thursday, December 1, 11

Error Handling

• Don’t return Null !

30

30Thursday, December 1, 11

Error Handling

• What do you think about this code ?

31

31Thursday, December 1, 11

Error Handling

• How to make code look like that ?

• Special Case object !

32

32Thursday, December 1, 11

Side Effects

• Consider the following class :

33

33Thursday, December 1, 11

Side Effects

• Consider the following code :

• Do you see the problem ?

34

34Thursday, December 1, 11

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

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

Side Effects

37

37Thursday, December 1, 11

Side Effects

• What this code display ?

38

38Thursday, December 1, 11

Side Effects

39

39Thursday, December 1, 11

Side Effects

40

40Thursday, December 1, 11

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

Side Effects

• Favor composition over inheritance !

42

42Thursday, December 1, 11

REFERENCESDo you want to read more about the subject ?

43

43Thursday, December 1, 11

Books

44

Clean CodeA Handbook of Agile Software Craftsmanship

Robert C. Martin (aka. Uncle Bob)

Prentice Hall editions

44Thursday, December 1, 11

Books

45

Effective JavaSecond Edition

Joshua Bloch

Addison Wesley editions

45Thursday, December 1, 11

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

top related