A brief overview about writing clean code. Presentation made for the Multimedia Languages and Environments course at Politecnico di Torino (academic year 2012/2013).
3. The only valid measurement: WTFs/min
3
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
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. 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. 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. What we have done
12
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. 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. Another example…
14
class Customer
{
private Date generationTimeStamp;
private Date modificationTimeStamp;
private final String recordId = “102”;
/* ... */
}
Better?
Clean Code 07/03/2013
16. Functions
16
Small
Do One Thing
Clean Code 07/03/2013
17. Do One Thing
17
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. Do One Thing
18
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
21. Explain yourself in the code
21
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. Explain yourself in the code
22
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. API Documentation
YES Explanation of intent
Clarification
Warning of consequences
Orphan comments
NO Obsolete comments
Noise comments
Code commented-out
23 Clean Code 07/03/2013
25. Formatting
25
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. Breaking Indentation
26
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. Breaking Indentation
27
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
29. Conventions
29
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. References
30
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. License
31
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