Coding Horror

programming and human factors

VS.NET and Code Regions

I'm currently working on a project where almost every function has its own region. At first I found this convention onerous, but as I used it, I saw why it was necessary. The default Visual Studio .NET outlining support leaves a lot to be desired. Take your typical commented Page_Load method:

/// <summary>
/// This method is called when the Page's Load event has been fired.
/// </summary>
/// <param name="sender">The <see cref="object"/> that fired the event.</param>
/// <param name="e">The <see cref="EventArgs"/> of the event.</param>
private void Page_Load(object sender, System.EventArgs e)  
if (!Page.IsPostBack)  
// do stuff

That function will outline in two blocks: one for the XML comments, and another for the actual code itself:

Page_Load method using default outlining

It's aggravating that there is no built in support for outlining the entire function and the comments. Hence, the need for a simple region around the comments and the function, which provides the outlining support you might expect in the first place:

Page_Load method using custom region

I just checked, and VS.NET 2005 (aka Whidbey) has this same bad behavior. What a bummer. I am working on a macro that Region-izes all the functions in a file, but in the meantime you might want to check out the Documentator macros on CodeProject. They combine commenting and regionizing into one function, which isn't always what I want, but it's close enough to start.

Written by Jeff Atwood

Indoor enthusiast. Co-founder of Stack Overflow and Discourse. Disclaimer: I have no idea what I'm talking about. Find me here: