Geeks With Blogs

News Coming soon!

Code Without Fear Ruminations by Don Huff
#region -- Foo Property --
private Foo Foo
{
    get
    {
        if (_foo == null)
        {
            _foo = FooManager.Instance.Load();
        }

        return _foo;
    }
}
#endregion

"Moderation in all things, including moderation." -Petronius

Code without fear!

--Don

Posted on Friday, July 6, 2012 10:49 AM CSharp , Smells | Back to top


Comments on this post: Code Smells: Regions in C#

# re: Code Smells: Regions in C#
Requesting Gravatar...
Post title contradicts the quote Don. Moderation is one thing.. all for that. Using regions is not a code smell. It helps organize code. Using regions like in your sample, yeah, all against that.
Left by Gabriel Rodriguez on Jul 09, 2012 11:51 AM

# re: Code Smells: Regions in C#
Requesting Gravatar...
@Gabriel...well put. My intention was to highlight how the overuse of "helpful" language features can reduce the signal-to-noise ratio of code as well as take a light-hearted poke at a colleague!
Left by Don on Jul 09, 2012 12:25 PM

# re: Code Smells: Regions in C#
Requesting Gravatar...
You've forgotten the comment on the Foo property. Any well-coded system needs comments. Fortunately, I've gone the extra mile and written it for you:

///<summary>
/// Gets Foo
///</summary>
Left by Sean on Jul 09, 2012 4:07 PM

# re: Code Smells: Regions in C#
Requesting Gravatar...
StyleCop sums up my feelings about regions quite nicely:


SA1109: BlockStatementsMustNotContainEmbeddedRegions

Cause: A C# statement contains a region tag between the declaration of the statement and the opening curly bracket of the statement.

Description: A violation of this rule occurs when the code contains a region tag in between the declaration and the opening curly bracket. This will result in the body of the statement being hidden when the region is collapsed.


SA1123: DoNotPlaceRegionsWithinElements

Cause: The C# code contains a region within the body of a code element.

Description: A violation of this rule occurs whenever a region is placed within the body of a code element. In many editors, including Visual Studio, the region will appear collapsed by default, hiding the code within the region. It is generally a bad practice to hide code within the body of an element, as this can lead to bad decisions as the code is maintained over time.


SA1124: DoNotUseRegions

Cause: The C# code contains a region.

Description: A violation of this rule occurs whenever a region is placed anywhere within the code. In many editors, including Visual Studio, the region will appear collapsed by default, hiding the code within the region. It is generally a bad practice to hide code, as this can lead to bad decisions as the code is maintained over time.


I agree with the title of your post: regions are most definitely a code smell. If you still can't find what you're looking for at a quick glance after collapsing all of the members in a file to their definitions (Ctrl+M, Ctrl+O), then your code probably needs to be refactored. There is almost always a way to increase the readability of a file without adding regions.
Left by Justin on Jul 09, 2012 7:59 PM

Your comment:
 (will show your gravatar)


Copyright © codeWithoutFear | Powered by: GeeksWithBlogs.net