Clean Code (PDF version)

Post on 04-Dec-2014

12.240 views 9 download

description

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

Transcript of Clean Code (PDF version)

Clean Code

Hendrik Ebel10. Mar. 2009

1

What is Clean Code?One Question ...

3

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

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

Motivation

The Total Cost of Owning a Mess

productivity vs. time

6

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

Meaningful Names

8

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

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

9

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

Comments

11

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

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

Functions

14

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

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

16

Objects and Data Structures

17

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

18

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

Error Handling

20

Error Handling

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

21

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

Unit Tests

23

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

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

FastIndependentRepeatable (in any environment)Self-ValidationTimly

25

Thanks - Any Questions?

26

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