Clean Code    Hendrik Ebel   10. Mar. 2009                   1
TopicsMotivationMeaningful NamesCommentsFunctions                              By Robert C. Martin & Co.Objects and Data S...
What is Clean Code?One Question ...                      3
What is Clean Code?   One Question ...                              elegant                                           effi...
What is Clean Code?   One Question ...                              elegant                                           effi...
MotivationThe Total Cost of Owning a Mess          productivity vs. time                                  6
Aims of Clean Codeproducing better code@author writing for readerscode has to be kept clean over time„Leave the campground...
Meaningful Names                   8
Meaningful Namesvariable, function or class names shouldanswer all the big questions: why it exists? what it does? how it ...
Meaningful NamesIf a name requires a comment, then thename does not reveal its intentavoid disinformation dont use type in...
Comments           11
Good and Bad Comments•Public API Comments     •Redundant Comments•Legal Comments          •Noise Comments•Explanation of I...
Comments„Purpose of a comment is to explain codethat does not explain itself.“Comments do not make up for bad codeDon‘t us...
Functions            14
FunctionsThe goal is to tell the story of thesystem.„The first rule of functions is that theyshould be small.“Do One Thing...
FunctionsIdeal number of arguments is zeroMore than three should‘t be usedanywayFlag arguments are ugly.Avoid output argum...
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 meaningfulfunctionseasy to add new behaviorshard to add new data structuresChoose t...
Error Handling                 20
Error HandlingWrite code that is clean and rebustSee error handling as a separate concernUse exceptions rather than return...
Error HandlingDon‘t return NULL throwing an exception or a special case object like „Collections.emptyList()“Don‘t pass NU...
Unit Tests             23
Unit TestsTest Driven Development (TDD) test and production code are written together tests just a few seconds aheadKeepin...
Unit TestsOne Assert or Single Concept per TestF.I.R.S.T.  Fast  Independent  Repeatable (in any environment)  Self-Valida...
Thanks - Any Questions?                          26
SourcesBook  „Clean Code“ by Robert C. Martin  ISBN: 0132350882Images  http://www.failblog.org  http://www.flickr.com/phot...
Upcoming SlideShare
Loading in...5
×

Clean Code (PDF version)

17,349

Published on

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

Published in: Technology

Clean Code (PDF version)

  1. 1. Clean Code Hendrik Ebel 10. Mar. 2009 1
  2. 2. TopicsMotivationMeaningful NamesCommentsFunctions By Robert C. Martin & Co.Objects and Data Structures Object Mentor Inc.Error HandlingUnit Tests 2
  3. 3. What is Clean Code?One Question ... 3
  4. 4. What is Clean Code? One Question ... elegant efficient simple and direct readability easy to enhance like a well-written prose care no duplications was made for the problem...many answers! 4
  5. 5. What is Clean Code? One Question ... elegant efficient simple and direct readability easy to enhance like a well-written prose care no duplications was made for the problem...many answers! 5
  6. 6. MotivationThe Total Cost of Owning a Mess productivity vs. time 6
  7. 7. Aims of Clean Codeproducing better code@author writing for readerscode has to be kept clean over time„Leave the campground cleaner thanyou found it.“ 7
  8. 8. Meaningful Names 8
  9. 9. Meaningful Namesvariable, function or class names shouldanswer all the big questions: why it exists? what it does? how it is used? 9
  10. 10. Meaningful NamesIf a name requires a comment, then thename does not reveal its intentavoid disinformation dont use type information in names (example: personList) Spelling similar concepts similarly is information. Using inconsistent spellings is disinformation. 10
  11. 11. Comments 11
  12. 12. Good and Bad Comments•Public API Comments •Redundant Comments•Legal Comments •Noise Comments•Explanation of Intent •Position Markers•Warning for •Closing Brace Comments Consequences •Commented-Out Code•real TODO Comments •Obsolete Comments •Nonpublic JavaDocs 12
  13. 13. Comments„Purpose of a comment is to explain codethat does not explain itself.“Comments do not make up for bad codeDon‘t use a comment when you can use afunction or a variableComments can contains lies 13
  14. 14. Functions 14
  15. 15. FunctionsThe goal is to tell the story of thesystem.„The first rule of functions is that theyshould be small.“Do One Thing!Stepdown Rule 15
  16. 16. FunctionsIdeal number of arguments is zeroMore than three should‘t be usedanywayFlag arguments are ugly.Avoid output argumentsSide effects are lies. 16
  17. 17. Objects and Data Structures 17
  18. 18. Objectshide data and expose functionseasy to add new objectshard to add new behaviors 18
  19. 19. Data Structuresexpose data and have no meaningfulfunctionseasy to add new behaviorshard to add new data structuresChoose the approach that is best for the job. 19
  20. 20. Error Handling 20
  21. 21. Error HandlingWrite code that is clean and rebustSee error handling as a separate concernUse exceptions rather than return codesUse unchecked exceptions 21
  22. 22. Error HandlingDon‘t return NULL throwing an exception or a special case object like „Collections.emptyList()“Don‘t pass NULL InvalidArgumentExceptions or assert better: forbid passing NULL by default 22
  23. 23. Unit Tests 23
  24. 24. Unit TestsTest Driven Development (TDD) test and production code are written together tests just a few seconds aheadKeeping tests cleanTest code is just as important asproduction code. 24
  25. 25. Unit TestsOne Assert or Single Concept per TestF.I.R.S.T. Fast Independent Repeatable (in any environment) Self-Validation Timly 25
  26. 26. Thanks - Any Questions? 26
  27. 27. SourcesBook „Clean Code“ by Robert C. Martin ISBN: 0132350882Images http://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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×