Outline – Focus On Spot, Define Boundaries

it takes all kinds to make this world


Original image credits – left, right

To be, or not to be, that is the question

It is indeed a personal choice (or your company’s directives) to use block outlining, aka regions, in your code. i have had my share of arguments, for and against the topic. If you want to get enlightened(?) on use of block outlining, tap here, here. This post is not about discussing whether or not you should use regions, this is only about why to use them in your code and how.

Why?
It is true that when your code grows bigger you should think about restructuring/refactoring, but a real-world class/method with only a slightly complex functionality has multiple physical/logical code blocks. i find outlining very handy and productive here. It helps me give a bird’s-eye view of my code and i can jump right in where i intend to, quickly and precisely. A visually structured code file makes it easier to focus on the area you are concerned with. Such code is more readable, comprehensive, and easy to update.

How?
When you are using Outlining feature of Visual Studio, it outlines classes, methods, conditions, etc for you. With #region you can make a block of code participate in outlining and you can expand or collapse it as you would other outlined sections of your code. Following is the syntax for outlining a block of your code:

#region [name]
#endregion

i personally prefer the style where #endregion also has the name repeated, like this:

#region [name]
#endregion [//name]

Though optional, but having name to describe the block of code with #region and #endregion makes it easier to resolve issues related to mismatched outlining. To avoid typing #region #endregion syntax repetitively and enforce standards, i suggest to have a code snippet to insert block outlining. This is my version of the code snippet, it prompts for name and automatically fills the name in #region and #endregion lines:

<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
          <Shortcut>RegionWrapper</Shortcut>
            <Title>
                RegionWrapper
            </Title>
        </Header>
        <Snippet>
          <Declarations>
            <Literal>
              <ID>RegionName</ID>
              <Type>string</Type>
              <ToolTip>Type region name</ToolTip>
              <Default>"region name"</Default>
            </Literal>
          </Declarations>
		
          <Code Language="CSharp">
    <![CDATA[        #region $RegionName$
        #endregion //$RegionName$
]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

If you want to first write your code and then select the code block and wrap it within region, you can use macro.

Following two keyboard shortcuts are most important while dealing with outlining:

ctrl+m+m Toggle Outlining Expansion
ctrl+m+l Toggle All Outlining
(For other shortcuts see the menu item Edit > Outlining in VS.)

To give you an idea about how outlining makes code less daunting, see following VS Snips of the same class (a dummy class derived from a real-world class), collapsed and expanded on regions:

VS Snip – Regions Expanded(this snip is also equivalent to having no outlining at all)

The same code in the above Snip, when collpased looks more accesible and cleaner.
VS Snip – Regions Collapsed

Crave for more, go here, here.

Enjoy #region-ing!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s