Clean Code (PDF version)

27
Clean Code Hendrik Ebel 10. Mar. 2009 1

description

Slides about Robert C. Martins book "Clean Code: A Handbook of Agile Software Craftsmanship". (PDF version)

Transcript of Clean Code (PDF version)

Page 1: Clean Code (PDF version)

Clean Code

Hendrik Ebel10. Mar. 2009

1

Page 3: Clean Code (PDF version)

What is Clean Code?One Question ...

3

Page 4: Clean Code (PDF version)

What is Clean Code?One Question ...

elegantefficient

easy to enhance

simple and direct

readabilitylike a well-written prose

careno duplications

was made for the problem...many answers!

4

Page 5: Clean Code (PDF version)

What is Clean Code?One Question ...

elegantefficient

easy to enhance

simple and direct

readabilitylike a well-written prose

careno duplications

was made for the problem...many answers!

5

Page 6: Clean Code (PDF version)

Motivation

The Total Cost of Owning a Mess

productivity vs. time

6

Page 7: Clean Code (PDF version)

Aims of Clean Code

producing better code@author writing for readerscode has to be kept clean over time„Leave the campground cleaner than you found it.“

7

Page 8: Clean Code (PDF version)

Meaningful Names

8

Page 9: Clean Code (PDF version)

Meaningful Namesvariable, function or class names should answer all the big questions:

why it exists?what it does?how it is used?

9

Page 10: Clean Code (PDF version)

Meaningful NamesIf a name requires a comment, then the name does not reveal its intentavoid disinformation

don't use type information in names (example: personList)Spelling similar concepts similarly is information. Using inconsistent spellings is disinformation.

10

Page 11: Clean Code (PDF version)

Comments

11

Page 12: Clean Code (PDF version)

Good and Bad Comments•Public API Comments•Legal Comments•Explanation of Intent•Warning for

Consequences•real TODO Comments

•Redundant Comments•Noise Comments•Position Markers•Closing Brace Comments•Commented-Out Code•Obsolete Comments•Nonpublic JavaDocs

12

Page 13: Clean Code (PDF version)

Comments

„Purpose of a comment is to explain code that does not explain itself.“Comments do not make up for bad codeDon‘t use a comment when you can use a function or a variableComments can contains lies

13

Page 14: Clean Code (PDF version)

Functions

14

Page 15: Clean Code (PDF version)

FunctionsThe goal is to tell the story of the system.„The first rule of functions is that they should be small.“Do One Thing!Stepdown Rule

15

Page 16: Clean Code (PDF version)

FunctionsIdeal number of arguments is zero More than three should‘t be used anywayFlag arguments are ugly.Avoid output argumentsSide effects are lies.

16

Page 17: Clean Code (PDF version)

Objects and Data Structures

17

Page 18: Clean Code (PDF version)

Objectshide data and expose functionseasy to add new objectshard to add new behaviors

18

Page 19: Clean Code (PDF version)

Data Structuresexpose data and have no meaningful functionseasy to add new behaviors hard to add new data structures

Choose the approach that is best for the job.

19

Page 20: Clean Code (PDF version)

Error Handling

20

Page 21: Clean Code (PDF version)

Error Handling

Write code that is clean and rebustSee error handling as a separate concernUse exceptions rather than return codesUse unchecked exceptions

21

Page 22: Clean Code (PDF version)

Error HandlingDon‘t return NULL

throwing an exception or a special case object like „Collections.emptyList()“

Don‘t pass NULLInvalidArgumentExceptions or assert better: forbid passing NULL by default

22

Page 23: Clean Code (PDF version)

Unit Tests

23

Page 24: Clean Code (PDF version)

Unit TestsTest Driven Development (TDD)

test and production code are written togethertests just a few seconds ahead

Keeping tests cleanTest code is just as important as production code.

24

Page 25: Clean Code (PDF version)

Unit TestsOne Assert or Single Concept per TestF.I.R .S.T.

FastIndependentRepeatable (in any environment)Self-ValidationTimly

25

Page 26: Clean Code (PDF version)

Thanks - Any Questions?

26

Page 27: Clean Code (PDF version)

Sources Book

„Clean Code“ by Robert C. Martin ISBN: 0132350882

Imageshttp://www.failblog.org http://www.flickr.com/photos/hugovk/199425487/http://www.flickr.com/photos/jackpot321/1809424991/http://www.osnews.com/story/19266/WTFs_m

27