Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

Documenting source code

Source code documenting is very important task. Code documentation servers as persisted communication between developers who are using same code. Undocumented or poorly documented code may be hard to understand to other developers who have then waste their valuable time to struggling through the code they don’t understand well. In this posting I introduce you some code documenting tips.

exclamationNote. I describe here code documentation you can produce using Visual Studio IDE. I don’t cover here documentations written for print media on word processors.

Worst practices

I have some awful experiences on working with undocumented and poorly documented code. When some library starts throwing unknown exceptions and all information you get is that Controller<SomeUnknownType>.KinkyMethod<SomeOtherType>() did something unexpected you are in trouble.

Here is one reality-show example.


/// <summary>

/// Shows the messages.

/// </summary>

/// <param name="messages">The messages.</param>

public void ShowMessages(string[] messages) { … }


You need some documentation to find out what different points in code flow do and how they act. If you don’t have documentation or you have documentation that sais almost nothing useful you have to invest your time to discover what goes wrong and why. I think we all know how time consuming these problems are.

Documentation basics

Documentation, what ever it is about, must be short and clear. If somebody else reads it he or she must be able to understand quickly what documented item exactly does and what behavior to expect. If class is written by you then you know exactly how it works and why it works this way. Other developers may not have this knowledge and they may not find your class name and method names intuitive.

Of course, one may argue that ideal code is self-describing etc. The problem is simple – we are not living in ideal world. So our code is never ideal and we are never ideal. Even if two people agree on something they usually don’t understand things exactly same way.

Interface documentation should describe:

  • purpose of interface,
  • expected usage of interface.

Class documentation should describe:

  • purpose of class,
  • expected usage of class,
  • context in which class is used,
  • implementation details for developers who extend the class.

Method/property documentation should describe:

  • what method/property does,
  • what method of property returns,
  • parameters of method,
  • are there both getter and setter for property,
  • exceptions that method/property directly throws.

My list is not long but it gives you some idea about what code documentation should contain. You may always find people who argue that code documentation is not needed. I don’t believe them. Don’t forget one thing – when you work on your old code again after three months or year then you get one new role: you are also client of your code because you don’t remember all the details anymore.


kick it on DotNetKicks.com pimp it Progg it 顶 Shout it

Comments

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# August 4, 2009 8:02 PM

DotNetBurner - Tips & Tricks said:

DotNetBurner - burning hot .net content

# August 4, 2009 8:03 PM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# August 4, 2009 8:04 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# August 4, 2009 8:05 PM

progg.ru said:

Thank you for submitting this cool story - Trackback from progg.ru

# August 4, 2009 8:06 PM

9eFish said:

9efish.感谢你的文章 - Trackback from 9eFish

# August 4, 2009 8:07 PM

Martin Hey said:

Thanks for this post. I often see such code documentations. In most cases it's generated by a tool. Then the method (or class) seems to be documented but it's not very useful. So hopefully many people will consider what you've described here.

# August 5, 2009 12:52 AM

DigiMortal said:

Thanks for feedback, Martin! :)

I think that code documenting tools are commonly misunderstood. They provide only templates of code documentation, not documentation itself.

# August 5, 2009 4:12 AM

zoldello said:

This blog is well-intented but not highly informative. It would be nice to see  examples of a well commented class, interface and properties. Plus, examples would make some things clearer. For example, you said class comments should include: "expected usage of class" and "context in which class is used". These would make sense if I had examples.  

Additionally, if commenting is so good why don't all Software Engineers use it. TIME! You ought to explain how your ideas make it worth the extra time which Software Engineers lack.

# August 5, 2009 12:58 PM

DigiMortal said:

Thanks for feedback and honest criticism, zoldello. I really appreciate it :)

I tried to put examples here but the draft posting got way too long to be interesting. I think I write some other posting(s) with examples of documentation so I can take better focus to different small(er) issues.

About time it takes to document source code. I have worked couple of times on codebase that is not documented or is documented poorly. I lost couple of weeks my time just because there was no information.

If you write code documentation when you add new property or method then it doesn't remarkable time because you are already "there". Documenting code in the end of development cycle is nightmare because there is not enough time for that. It takes then longer because things you have to document are not freshly in your head anymore.

# August 5, 2009 2:45 PM

Mark said:

I think nowadays thorough documentation is a pointless exercise. I would much rather have developers code in a readable, maintainable manner, than double the code base by having to explain their intentions.

If a function or method is getting to the point where each of its actions required a line comment to explain itself, then you're not adherring to the Single Responsibility Principle.

In other words, your method is doing too much.

I appreciate class/interface/method documentation, but thats as far as it should go. Any comments inside the actual methods just means that the code you've written is unreadable.

# August 8, 2009 11:01 AM

gOODiDEA.NET said:

.NET Fun With the ?? Operator in C#: if { } or ?? – Which is Faster? Documenting source code Dictionary&lt;T&gt;

# August 9, 2009 8:37 PM

gOODiDEA said:

.NETFunWiththe??OperatorinC#:if{}or??–WhichisFaster?Documentingsourcecode...

# August 9, 2009 8:37 PM

ComponentGear.com Feed said:

This week on C9, Brian and Habib &quot;Runtime&quot; Heydarian review the week&#39;s top developer stories

# August 14, 2009 3:33 PM

John said:

Mark:

  I agree to a point but not completely.  The method that looks like this

int foo () {

  // do a

   ...

  // do b

   ...

  // do c

  ...

}

is definitely an antipattern, but there are other, good reasons for comments inside a method.  Sometimes a method does something in a confusion or non-obvious way, but for a good reason.  Some concepts, like the idea that a sequence of statements has strong ordering constraints, are not redily expressable in most programming languages.  Sometimes I but comments arround bug fixes -- especially if its a bug I've made more than once.

Its easy when you write small demo programs to confidently state that there are hard and fast rules that should never be broken.  Real engineering is a process of compromise, and just about every rule gets broken eventually.  A well placed comment can ameliorate a lot of the damage from doing so.

# August 15, 2009 5:29 AM

DigiMortal said:

Thanks for comment, John!

I completely agree with you and I should also make a note that your comment is addition, not a fix, to this posting. :)

If there is something tricky or something that works only the way as it is now then comment in code is must be. Otherwise code gets messed up and old bugs that were once fixed arrive again.

# August 15, 2009 7:03 AM

This Week C9: Win7 API’s, Spec# and Boogie, Expression 3 Starter Kits | PSP NEWS said:

Pingback from  This Week C9: Win7 API&#8217;s, Spec# and Boogie, Expression 3 Starter Kits | PSP NEWS

# August 20, 2009 8:28 PM

This Week C9: Win7 API’s, Spec# and Boogie, Expression 3 Starter Kits | PSP NEWS said:

Pingback from  This Week C9: Win7 API&#8217;s, Spec# and Boogie, Expression 3 Starter Kits | PSP NEWS

# August 20, 2009 8:28 PM

This Week C9: Win7 API’s, Spec# and Boogie, Expression 3 Starter Kits | CHARGED's Digital Lifestyle at Work or Play said:

Pingback from  This Week C9: Win7 API&#8217;s, Spec# and Boogie, Expression 3 Starter Kits | CHARGED's Digital Lifestyle at Work or Play

# August 25, 2009 2:38 AM