SlideShare a Scribd company logo
1 of 10
{
Comments
What to comment and not
{
// Check to see if the employee is eligible
for full benefits
if ((employee.flags & HOURLY_FLAG)
&& (employee.age > 65))
Or this?
if (employee.isEligibleForFullBenefits())
It takes only a few seconds
of thought to explain most
of your intent in code.
In many cases it’s simply a
matter of creating a
function that says the same
thing as the comment you
want to write.
“Don’t comment bad code – rewrite it”
– Brian W. Kernighan and P.J. Plaugher
Explain Yourself in Code
{
 Some comments are necessary or
beneficial. We’ll look at a few.
 Keep in mind, however, that the
only truly good comment is the
comment you found a way not to
write
Good Comments
{
Good Comments
Legal Comments
Informative Comments
Explanation of Intent
Clarification
Warning of Consequences
TODO Comments
Amplification
{
// Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights
//reserved.
// Released under the terms of the GNU General Public License
//version 2 or later.
Sometimes our corporate
coding standards force us
to write certain comments
for legal reasons.
Legal
Comments
{
// Returns an instance of the Responder being tested.
protected abstract Responder responderInstance();
What about renaming the function?
responderBeingTested();
It is sometimes useful to
provide basic information
with a comment.
Informative
Comments
{public int compareTo(Object o)
{
if(o instanceof WikiPagePath)
{
WikiPagePath p = (WikiPagePath) o;
String compressedName = StringUtil.join(names, "");
String compressedArgumentName = StringUtil.join(p.names, "");
return compressedName.compareTo(compressedArgumentName);
}
return 1; // we are greater because we are the right type.
}
Sometimes a comment
goes beyond just useful
information about the
implementation and
provides the intent behind
a decision
Explanation of
Intent
{// Don't run unless you
// have some time to kill.
public void _testWithReallyBigFile()
{
writeLinesToFile(10000000);
response.setBody(testFile);
response.readyToSend(this);
String responseString = output.toString();
assertSubString("Content-Length: 1000000000", responseString);
assertTrue(bytesSent > 1000000000);
}
Sometimes it is useful
to warn other
programmers about
certain consequences.
Warning of
Consequences
{
//TODO-MdM these are not needed
// We expect this to go away when we do the checkout model
protected VersionInfo makeVersion() throws Exception
{
return null;
}
It is sometimes
reasonable to leave “To
do” notes in the form of
//TODO comments.
TODO
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.
new ListItemWidget(this, listItemContent, this.level + 1);
return buildList(text.substring(match.end()));
A comment may be used
to amplify the
importance of something
that may otherwise seem
inconsequential.
Amplification

More Related Content

Viewers also liked

Viewers also liked (10)

Revista digital 02084
Revista digital 02084Revista digital 02084
Revista digital 02084
 
Vzavalacrespo procedimientos
Vzavalacrespo procedimientosVzavalacrespo procedimientos
Vzavalacrespo procedimientos
 
Insectos
InsectosInsectos
Insectos
 
Naming Conventions
Naming ConventionsNaming Conventions
Naming Conventions
 
Vzavalacrespo final1
Vzavalacrespo final1Vzavalacrespo final1
Vzavalacrespo final1
 
Новости недвижимости Майами - Январь 2016
Новости недвижимости Майами - Январь 2016Новости недвижимости Майами - Январь 2016
Новости недвижимости Майами - Январь 2016
 
Medicos.pdf diana
Medicos.pdf dianaMedicos.pdf diana
Medicos.pdf diana
 
Site web conception
Site web conceptionSite web conception
Site web conception
 
Temporary Works Certificate
Temporary Works CertificateTemporary Works Certificate
Temporary Works Certificate
 
El portafolio-estudiantil (1)
El portafolio-estudiantil (1)El portafolio-estudiantil (1)
El portafolio-estudiantil (1)
 

Similar to Comments

[2012 02 03]clean_code 4장
[2012 02 03]clean_code 4장[2012 02 03]clean_code 4장
[2012 02 03]clean_code 4장
Jong Pil Won
 
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Applitools
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
Hammad Rajjoub
 

Similar to Comments (20)

[2012 02 03]clean_code 4장
[2012 02 03]clean_code 4장[2012 02 03]clean_code 4장
[2012 02 03]clean_code 4장
 
Clean Code
Clean CodeClean Code
Clean Code
 
Clean code
Clean codeClean code
Clean code
 
Clean code
Clean codeClean code
Clean code
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
 
Clean Code
Clean CodeClean Code
Clean Code
 
Practices For Becoming A Better Programmer
Practices For Becoming A Better ProgrammerPractices For Becoming A Better Programmer
Practices For Becoming A Better Programmer
 
Wtf per lineofcode
Wtf per lineofcodeWtf per lineofcode
Wtf per lineofcode
 
Oop
OopOop
Oop
 
Clean Code 2
Clean Code 2Clean Code 2
Clean Code 2
 
Clean code
Clean codeClean code
Clean code
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
 
Modern Python Testing
Modern Python TestingModern Python Testing
Modern Python Testing
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
 
Internet programming slide - java.ppt
Internet programming slide - java.pptInternet programming slide - java.ppt
Internet programming slide - java.ppt
 
Naming Standards, Clean Code
Naming Standards, Clean CodeNaming Standards, Clean Code
Naming Standards, Clean Code
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScript
 

Comments

  • 2. { // Check to see if the employee is eligible for full benefits if ((employee.flags & HOURLY_FLAG) && (employee.age > 65)) Or this? if (employee.isEligibleForFullBenefits()) It takes only a few seconds of thought to explain most of your intent in code. In many cases it’s simply a matter of creating a function that says the same thing as the comment you want to write. “Don’t comment bad code – rewrite it” – Brian W. Kernighan and P.J. Plaugher Explain Yourself in Code
  • 3. {  Some comments are necessary or beneficial. We’ll look at a few.  Keep in mind, however, that the only truly good comment is the comment you found a way not to write Good Comments
  • 4. { Good Comments Legal Comments Informative Comments Explanation of Intent Clarification Warning of Consequences TODO Comments Amplification
  • 5. { // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights //reserved. // Released under the terms of the GNU General Public License //version 2 or later. Sometimes our corporate coding standards force us to write certain comments for legal reasons. Legal Comments
  • 6. { // Returns an instance of the Responder being tested. protected abstract Responder responderInstance(); What about renaming the function? responderBeingTested(); It is sometimes useful to provide basic information with a comment. Informative Comments
  • 7. {public int compareTo(Object o) { if(o instanceof WikiPagePath) { WikiPagePath p = (WikiPagePath) o; String compressedName = StringUtil.join(names, ""); String compressedArgumentName = StringUtil.join(p.names, ""); return compressedName.compareTo(compressedArgumentName); } return 1; // we are greater because we are the right type. } Sometimes a comment goes beyond just useful information about the implementation and provides the intent behind a decision Explanation of Intent
  • 8. {// Don't run unless you // have some time to kill. public void _testWithReallyBigFile() { writeLinesToFile(10000000); response.setBody(testFile); response.readyToSend(this); String responseString = output.toString(); assertSubString("Content-Length: 1000000000", responseString); assertTrue(bytesSent > 1000000000); } Sometimes it is useful to warn other programmers about certain consequences. Warning of Consequences
  • 9. { //TODO-MdM these are not needed // We expect this to go away when we do the checkout model protected VersionInfo makeVersion() throws Exception { return null; } It is sometimes reasonable to leave “To do” notes in the form of //TODO comments. TODO Comments
  • 10. { 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. new ListItemWidget(this, listItemContent, this.level + 1); return buildList(text.substring(match.end())); A comment may be used to amplify the importance of something that may otherwise seem inconsequential. Amplification

Editor's Notes

  1. Nowadays, of course, we’d turn off the test case by using the @Ignore attribute with an appropriate explanatory string. @Ignore("Takes too long to run"). But back in the days before JUnit 4, putting an underscore in front of the method name was a common convention. The comment, while flippant, makes the point pretty well.