3. The only valid measurement of code quality:
WTFs/minute
05.02.2013 3 www.consol.com
4. What is Clean Code?
§ Simple § Efficient
§ No duplications § Elegant
§ Direct § Focused
§ With tests § Minimal dependencies
§ It was written by someone who cares
§ ...
§ Any fool can write code that a computer can
understand. Good programmers write code that
humans can understand. - Martin Fowler
05.02.2013 4 www.consol.com
5. Why is clean code so important?
§ Easier to read
§ Easier to understand
§ Easier to change
§ Add new features
§ Find and fix bugs
§ Cheaper to maintain
§ Code quality is part of software quality
05.02.2013 5 www.consol.com
6. Use Intention-Revealing Names
§ int d;!
§ int days;!
§ int days; //days since creation!
§ int daysSinceCreation;!
05.02.2013 6 www.consol.com
7. Use Intention-Revealing Names
§ What is the purpose?
public List<int[]> getValues() {!
List<int[]> list1 = new ArrayList<int[]>();!
for (int[] x : theList)!
if (x[0] == 4) list1.add(x);!
return list1;!
}!
05.02.2013 7 www.consol.com
8. Use Intention-Revealing Names
§ Better?
!
public List<Cell> getFlaggedCells() {!
List<Cell> flaggedCells = new ArrayList<Cell>();!
for (Cell cell : gameBoard) !
if (cell.isFlagged()) flaggedCells.add(cell);!
return flaggedCells;!
}!
!
05.02.2013 8 www.consol.com
9. Don´t talk in code – Use Pronounceable Names
class DtaRcrd102 {
private Date genymdhms;
private Date modymdhms;
private final String pszqint = "102"; /* ... */ !
}!
§ Better?
class Customer {
private Date generationTimestamp; !
private Date modificationTimestamp; !
private final String recordId = "102"; /* ... */ !
}!
05.02.2013 9 www.consol.com
10. More recommendations...
§ Class names
Customer, Account, AddressParser, ...
§ Method names
postPayment, deletePage, ....
§ When constructors are overloaded...
new Complex(23.0);!
Complex.fromRealNumber(23.0); !
§ Pick one word per concept
get, fetch, retrieve...
§ Don´t be cute. Jokes out of the code.
public void saveTheUniverse() {...}
05.02.2013 10 www.consol.com
11. Bad Comments
§ Redundant, noise, irrelevant, obsolet, „funny“ comments
/**!
* Returns the day of the month. *!
* @return the day of the month. */!
public int getDayOfMonth() {!
return dayOfMonth;!
}!
!
/** Always return true. **/!
public boolean isAvailable() {!
!return false;!
}!
!
//Magic. Do not touch !
/* Added by Ana Cortés (4-4-2008) */!
05.02.2013 11 www.consol.com
12. Bad Comments
/** !
* Dear maintainer:!
* !
* Once you are done trying to 'optimize' this routine,!
* and have realized what a terrible mistake that was,!
* please increment the following counter as a warning!
* to the next guy:!
* !
* total_hours_wasted_here = 42!
**/!
§ Commented-Out Code
InputStreamResponse response = new InputStreamResponse();!
response.setBody(formatter.getResultStream(), ft.getByteCount()); !
// InputStream resultsStream = formatter.getResultStream(); !
// StreamReader reader = new StreamReader(resultsStream); !
// response.setContent(reader.read(formatter.getByteCount())); !
05.02.2013 12 www.consol.com
13. Good Comments
§ Legal comments
// Copyright (C) 2003 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the GNU General Public License version 2 or later. !
§ Informative, clarification comments
// format matched kk:mm:ss EEE, MMM dd, yyyy !
Pattern timeMatcher = Pattern.compile("d*:d*:d* w*, w* d*, d*"); !
§ Amplification comments
String listItemContent = match.group(3).trim(); !
// the trim is real important. It removes the starting !
// spaces that could cause the item to be recognized
// as another list.
§ TODO comments
§ Javadocs in Public APIs
05.02.2013 13 www.consol.com
24. Summary
§ Read „Clean Code“ book.
§ Be a little more careful with the code you write
§ Write with the „audience“ in mind
§ Follow the „Boy Scout Rule“
§ Leave the campground cleaner than you found it
§ Use tools that help you cleaning up
§ Write Clean Tests
05.02.2013 24 www.consol.com