Blog Stats
  • Posts - 24
  • Articles - 0
  • Comments - 61
  • Trackbacks - 92

 

Use C# comment styles to your advantage

Comments within a C# method body tend to A) make a problem statement then B) document the steps to solve the problem.  The problem statements tend to be multi-line; the details of the solution tend to be single-line. 

I want the problem statements to stand out.  Traditionally I use #region blocks to group the problems.  Unfortunately, regions are single-line.  They are also slightly more laborous to construct.

Lately I have been using a triple-slash comment block at the top of the method body to state the problem:

/// <summary>
/// Occurs when the page loads.
/// </summary>
private void Page_Load(object sender, System.EventArgs e)
{
    /// Configure the http response to return a correct set of
    /// cache headers. Validation headers must also be returned.

    // set the etag
    this.Response.Cache.SetETag("055aba934a3c21:380");

    // set the cache policy
    this.Response.Cache.SetCacheability(HttpCacheability.Public);
}

Another reason to use the triple-slash comment block within a method body, as opposed to the <remarks> block,  is that it does not form a part of the API documentation.  That is, the client-supplier relationship plays a role here.  It is desirable to separate interface documentation from implementation documentation - they are intended for two different audiences. For a given method the external documentation should be placed in a <remarks> block above the method declaration; the internal documentation should be placed in triple-comment blocks at the top of the method body.  It will naturally stand out from the implementation specifics. 

This approach works better than using /* */-style comments blocks because such comments cannot be nested.

Incidentally, VS.NET has a handy button on the Text Editor toolbar to comment out the selected lines.  It adds double-slash comments at the extreme left side.  A complementary button exists that will uncomment the selected lines.  It seems that left-justified comments are reserved for commenting-out code, not documentation.


Feedback

# re: Use C# comment styles to your advantage

Gravatar good idea 11/19/2004 1:05 PM | Brad

# re: Use C# comment styles to your advantage

Gravatar
不锈钢管
无缝钢管 12/11/2007 2:41 AM | bsknuisss

# re: Use C# comment styles to your advantage

Gravatar I like /* */-style 1/6/2008 6:27 AM | intranet

# re: Use C# comment styles to your advantage

Gravatar I like you ideas and enjoy your news letter but frankly i find you to be an asshole. Last night at dinner when you got mad at my wife and hit her in the teeth was completely unnacceptable. All she said was she didnt like your tie. I mean jesus christ. It was a big deal! At least she is used to getting bopped everyonce in a while! See at christmas brosef 6/3/2009 11:32 PM | tosha

# re: Use C# comment styles to your advantage

Gravatar 8.18Cwow power leveling 8/17/2009 9:30 PM | wow

# re: Use C# comment styles to your advantage

Gravatar
Nice article, very helpful.
Thanks!!

nike air
9/7/2009 8:18 AM | klecike

# re: Use C# comment styles to your advantage

Gravatar It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again. 10/19/2009 9:54 PM | links of london Sweetie Bracelet

# re: Use C# comment styles to your advantage

Gravatar nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again. 10/19/2009 10:10 PM | tiffany key ring

Post a comment





 

 

 

Copyright © Eron Wright