Chapter 04 - Comments
Chapter 04 - Comments
Clean Code:
A Handbook of
Agile Software Craftsmanship
− Comments are often unrecognized components of code that can confuse readers and lead to misunderstanding.
− Comments are used to cover for bad code, instead of fixing it.
− If code requires comments, it means, it can not express the author's intention clearly.
− Comments are often not updated along with the code.
− Codes get refactored, but comments don’t.
− Comments might become detached from the code they explain.
Although I'd like to say “don’t use comments”, it's not always practical. Because sometimes they can lead to value. So
we should consider some points about them.
Points around commenting
// BAD CODE
// Check to see if the employee is eligible for full benefits
if((employees.flags && HOURLY_FLAG) && (employees.age >65))
// GOOD CODE
if(employees.isEligibleForFullBenefits())
Points around commenting
// ACCEPTABLE
// This is our best attempt to get a race condition
// by creating large number of threads.
for (int i = 0; i< 25000; i++)
{
WidgetBuilderThread widgetBuilderThread = new WidgetBuilderThread(widgetBuilder, text, parent, failFlag);
Thread thread = new Thread(widgetBuilderThread);
thread.start();
}
Points around commenting
// ACCEPTABLE
assertTrue(a.compareTo(b) != 0); // a != b
assertTrue(ab.compareTo(ba) == 0); // ab == ba
Points around commenting
// ACCEPTABLE
// Don't run unless you have time to kill
public void testWithReallyBigFile() {
writeLinesToFile(10000000);
response.setBody(testFile);
response.readyToSend(this);
String responseString = output.toString();
assertSubString("Content-Length: 10000000", responseString);
assertTrue(bytesSent > 10000000);
}
Points around commenting
// ACCEPTABLE
//TODO: These method should be deleted
protected VersionInfo makeVersion() throws Exception {
return null;
}
Points around commenting