Best practice       Luigi De Russis                Clean Code
2       Measuring code qualityClean Code                       07/03/2013
The only valid measurement: WTFs/min3                 Good code               Bad code                                    ...
4       GoalsClean Code      07/03/2013
Code MUST be…5          Readable   Maintainable       Extendable      Testable    Clean Code                07/03/2013
6       How?Clean Code     07/03/2013
7   Clean Code   07/03/2013
8       Meaningful namesClean Code                 07/03/2013
An example…9    public List<int[]> getThem()    {        List<int[]> list = new ArrayList<int[]>();        for (int[] x : ...
An example…10     public List<int[]> getFlaggedCells()     {         List<int[]> flaggedCells = new ArrayList<int[]>();   ...
An example…11     public List<Cell> getFlaggedCells()     {         List<Cell> flaggedCells = new ArrayList<Cell>();      ...
What we have done12     Used intention           flaggedCells rather     revealing names          than list     Replaced m...
Another example…13     class DtaRcrd102     {         private Date genymdhms;         private Date modymdhms;         priv...
Another example…14     class Customer     {         private Date generationTimeStamp;         private Date modificationTim...
15       Functions Clean Code          07/03/2013
Functions16                     Small                  Do One Thing     Clean Code                  07/03/2013
Do One Thing17     public bool isEdible()     {         if(this.ExpirationDate > Date.Now &&                    this.Appro...
Do One Thing18     public bool isEdible()     {          return isFresh() &&                  isApproved() &&             ...
Functions19          Handle errors (and use exceptions)                  Don’t Repeat Yourself                  (avoid cop...
20       Comments Clean Code         07/03/2013
Explain yourself in the code21                        Which one is cleaner?        //check to see if the employee is eligi...
Explain yourself in the code22                        Which one is cleaner?        //check to see if the employee is eligi...
API Documentation     YES            Explanation of intent                    Clarification                    Warning of ...
24       Formatting Clean Code           07/03/2013
Formatting25                  Communication is the purpose of                           formatting        Vertical opennes...
Breaking Indentation26     public class CommentWidget extends TextWidget {         public static final String REGEXP =    ...
Breaking Indentation27     public class CommentWidget extends TextWidget     {        public static final String REGEXP = ...
28       Conventions Clean Code            07/03/2013
Conventions29                    Conventions enable common                          understanding      Stick to the langua...
References30        Robert C. Martin Series, “Clean Code - A Handbook         of Agile Software Craftsmanship”, Prentice ...
License31        This work is licensed under the Creative Commons “Attribution-         NonCommercial-ShareAlike Unported...
Upcoming SlideShare
Loading in …5
×

Clean Code

1,788 views

Published on

A brief overview about writing clean code. Presentation made for the Multimedia Languages and Environments course at Politecnico di Torino (academic year 2012/2013).

Published in: Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,788
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
54
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Clean Code

  1. 1. Best practice Luigi De Russis Clean Code
  2. 2. 2 Measuring code qualityClean Code 07/03/2013
  3. 3. The only valid measurement: WTFs/min3 Good code Bad code WTF, WTF WTF this is shit! CODE CODE REVIEW WTF REVIEW WTF WTF Dude, WTF WTF Clean Code 07/03/2013
  4. 4. 4 GoalsClean Code 07/03/2013
  5. 5. Code MUST be…5 Readable Maintainable Extendable Testable Clean Code 07/03/2013
  6. 6. 6 How?Clean Code 07/03/2013
  7. 7. 7 Clean Code 07/03/2013
  8. 8. 8 Meaningful namesClean Code 07/03/2013
  9. 9. An example…9 public List<int[]> getThem() { List<int[]> list = new ArrayList<int[]>(); for (int[] x : theList) if (x[0] == 4) list.add(x); return list; } What does this code do? Clean Code 07/03/2013
  10. 10. An example…10 public List<int[]> getFlaggedCells() { List<int[]> flaggedCells = new ArrayList<int[]>(); for (int[] cell : gameBoard) if (cell[STATUS_VALUE] == FLAGGED) flaggedCell.add(cell); return flaggedCells; } Better? Clean Code 07/03/2013
  11. 11. An example…11 public List<Cell> getFlaggedCells() { List<Cell> flaggedCells = new ArrayList<Cell>(); for (Cell cell : gameBoard) if (cell.isFlagged()) flaggedCell.add(cell); return flaggedCells; } What about this? Clean Code 07/03/2013
  12. 12. What we have done12 Used intention flaggedCells rather revealing names than list Replaced magic cell[STATUS_VALUE] numbers with constants rather than x[0] Created an appropriate Cell cell rather than abstract data type int[] cell Clean Code 07/03/2013
  13. 13. Another example…13 class DtaRcrd102 { private Date genymdhms; private Date modymdhms; private final String pszqint = “102”; /* ... */ } Use pronounceable and searchable names (please!) Clean Code 07/03/2013
  14. 14. Another example…14 class Customer { private Date generationTimeStamp; private Date modificationTimeStamp; private final String recordId = “102”; /* ... */ } Better? Clean Code 07/03/2013
  15. 15. 15 Functions Clean Code 07/03/2013
  16. 16. Functions16 Small Do One Thing Clean Code 07/03/2013
  17. 17. Do One Thing17 public bool isEdible() { if(this.ExpirationDate > Date.Now && this.ApprovedForConsumption == true && this.InspectorId != null) { return true; else return false; } How many things is the function doing? Clean Code 07/03/2013
  18. 18. Do One Thing18 public bool isEdible() { return isFresh() && isApproved() && isInspected(); } Now the function is doing one thing! A change in the specification turns into a single change in the code. Clean Code 07/03/2013
  19. 19. Functions19 Handle errors (and use exceptions) Don’t Repeat Yourself (avoid copy-and-paste code) Clean Code 07/03/2013
  20. 20. 20 Comments Clean Code 07/03/2013
  21. 21. Explain yourself in the code21 Which one is cleaner? //check to see if the employee is eligible for full benefits if((employee.flags & HOURLY_FLAG) && (employee.age > 65)) if(employee.isEligibleForFullBenefits()) Clean Code 07/03/2013
  22. 22. Explain yourself in the code22 Which one is cleaner? //check to see if the employee is eligible for full benefits if((employee.flags & HOURLY_FLAG) && (employee.age > 65)) if(employee.isEligibleForFullBenefits()) Clean Code 07/03/2013
  23. 23. API Documentation YES Explanation of intent Clarification Warning of consequences Orphan comments NO Obsolete comments Noise comments Code commented-out23 Clean Code 07/03/2013
  24. 24. 24 Formatting Clean Code 07/03/2013
  25. 25. Formatting25 Communication is the purpose of formatting Vertical openness between concepts Each blank line is a visual cue that identifies a new and separate concept Clean Code 07/03/2013
  26. 26. Breaking Indentation26 public class CommentWidget extends TextWidget { public static final String REGEXP = “^#[rn]*(?:(?:rn)|n|r)?”; public CommentWidget(String text) { super(text); } public String render() throws Exception { return “”; } } Eh? Clean Code 07/03/2013
  27. 27. Breaking Indentation27 public class CommentWidget extends TextWidget { public static final String REGEXP = “^#[rn]*(?:(?:rn)|n|r)?”; public CommentWidget(String text) { Better? super(text); } public String render() throws Exception { return “”; } } Clean Code 07/03/2013
  28. 28. 28 Conventions Clean Code 07/03/2013
  29. 29. Conventions29 Conventions enable common understanding Stick to the language-specific conventions Respect team-level conventions Still complying with the language-specific conventions… Clean Code 07/03/2013
  30. 30. References30  Robert C. Martin Series, “Clean Code - A Handbook of Agile Software Craftsmanship”, Prentice Hall  Geek and Poke, http://geekandpoke.typepad.com/  OSNews Comics, http://www.osnews.com/comics Clean Code 07/03/2013
  31. 31. License31  This work is licensed under the Creative Commons “Attribution- NonCommercial-ShareAlike Unported (CC BY-NC-SA 3,0)” License.  You are free:  to Share - to copy, distribute and transmit the work  to Remix - to adapt the work  Under the following conditions:  Attribution - You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).  Noncommercial - You may not use this work for commercial purposes.  Share Alike - If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.  To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ Clean Code 07/03/2013

×