Top Banner
Rules for documenting code
31

Commenting in Agile Development

Aug 06, 2015

Download

Technology

Jenda Rybák
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Commenting in Agile Development

Rules for documenting code

Page 2: Commenting in Agile Development

KISS

Page 3: Commenting in Agile Development

Keep it short and simple

Page 4: Commenting in Agile Development

Examples

Page 5: Commenting in Agile Development

Why do we use comments?

Page 6: Commenting in Agile Development

Why do we use comments?

Comments are failure.

Page 7: Commenting in Agile Development

Explain Yourself in Code//  check  if  the  user  is  administratorif  (user.isRegistered  &&  user.privileges.equals(‘admin’))  {  ...}

vs.if  (user.isAdmin)  {...}

Page 8: Commenting in Agile Development

What is so bad aboutcomments?

Page 9: Commenting in Agile Development

What is so bad aboutcomments?

They rot.

Page 10: Commenting in Agile Development

Inaccurate comments are worse than no comments at all.

/*  End  slide  */

Page 11: Commenting in Agile Development

Bad comments

Page 12: Commenting in Agile Development

Bad comments

...most of them.

Page 13: Commenting in Agile Development

Redundant Comments/**  *  The  Loader  implementation  with  which  this  Container  is  *  associated.  */  protected  Loader  loader  =  null;

/**  *  The  Logger  implementation  with  which  this  Container  is  *  associated.  */  protected  Log  logger  =  null;

/**  *  The  Manager  implementation  with  which  this  Container  is  *  associated.  */  protected  Manager  manager  =  null;

Page 14: Commenting in Agile Development

Redundant Comments

 protected  Loader  loader  =  null;  protected  Log  logger  =  null;  protected  Manager  manager  =  null;

Page 15: Commenting in Agile Development

Noise Comments/**  The  name  */private  String  name;

/**  The  version  */private  String  version;

/**  The  licence  */private  String  licence;

Page 16: Commenting in Agile Development

Scary Noise Comments/**  The  name  */private  String  name;

/**  The  version  */private  String  version;

/**  The  licence  */private  String  licence;

/**  The  licence  */private  String  info;

/**  The  licence  */private  String  help;

Page 17: Commenting in Agile Development

No Comments

private  String  name;private  String  version;private  String  licence;private  String  info;private  String  help;

Page 18: Commenting in Agile Development

Closing Brace Comments

try  {   while  ((line  =  in.readLine())  !=  null)  {   ...  

  }  //  while

}  //  try

Page 19: Commenting in Agile Development

Commented-out Code

public  class  Program{        static  void  Main(string[]  args)        {                /*  This  block  of  code  is  no  longer  needed                  *  because  we  found  out  that  Y2K  was  a  hoax                  *  and  our  systems  did  not  roll  over  to  1/1/1900  */                //DateTime  today  =  DateTime.Today;                //if  (today  ==  new  DateTime(1900,  1,  1))                //{                //        today  =  today.AddYears(100);                //        string  message  =  "The  date  has  been  fixed  for  Y2K.";                //        Console.WriteLine(message);                //}        }

}

Page 20: Commenting in Agile Development

HTML Tags in Comments/**  *  Task  to  run  fit  tests.  This  task  runs  fitnesse  tests  and  publishes  the  results.  *    *  <pre>  *  Usage:  *  &lt;taskdef  name=&quot;execute-­‐fitnesse-­‐tests&quot;  classname=&quot;fitnesse.ant.ExecuteFitnesseTestsTask&quot;  classpathref=&quot;classpath&quot;  /&gt;  *  OR  *  &lt;taskdef  classpathref=&quot;classpath&quot;  resource=&quot;tasks.properties&quot;  /&gt;  *    *  &lt;execute-­‐fitnesse-­‐tests  suitepage=&quot;FitNesse.SuiteAcceptanceTests&quot;  fitnesseport=&quot;8082&quot;  resultsdir=&quot;${results.dir}&quot;  resultshtmlpage=&quot;fit-­‐results.html&quot;  classpathref=&quot;classpath&quot;  /&gt;  *  </pre>  */

Page 21: Commenting in Agile Development

Good comments

Page 22: Commenting in Agile Development

Informative Comments

//  format  matched  kk:mm:ss  EEE,  MMM  dd,  yyyy

Pattern  timeMatcher  =  Pattern.compile(

“\\d*:\\d*:\\d*  \\w*,  \\w*  \\d*,  \\d*”);

Page 23: Commenting in Agile Development

Warning of Consequences Comments

//  Don’t  run  unless  you//  have  some  time  to  killpublic  void  _testWithReallyBigFile()  {   writeLinesToFile(100000000);

  response.setBody(testFile);   response.readyToSend(this);   String  responseString  =  output.toString();   assertSubString(“Content-­‐Length:  100000000”,                  responseString);   assertTrue(bytesSent  >  100000000)}

Page 24: Commenting in Agile Development

TODO Comments

//  TODO:  Refactor  this  code

Page 25: Commenting in Agile Development

TODO Comments

//  TODO:  Refactor  this  code

//  FIXME:  This  won't  work  if  the  file  is  missing.  

//  XXX:  This  method  badly  needs  refactoring:  should  switch  by  core  type.

Page 26: Commenting in Agile Development

Dynamic comments

Page 27: Commenting in Agile Development

Tests as Documentation

Unit tests = code level documentationBehaviour tests = feature documentation

Page 28: Commenting in Agile Development

Jnario.org

Page 29: Commenting in Agile Development

MessageTry to write clear self-explanatory code.

Avoid comments if possible.

Page 30: Commenting in Agile Development

Thanks

Page 31: Commenting in Agile Development

ReferencesProwareness bloghttp://www.prowareness.com/blog/anti-agile-manisfesto/

Clean Code (A Handbook of Agile Software Craftsmanship)Robert C. Martin

Jnario http://jnario.org/