Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Overuse of #Regions

Before I get started, I personally am a fan of using #regions in my code.  I think it cleans up everything, allowing me to only see what I want to see.  I also know that many of you disagree with me, and that is fine.

Here are 2 posts that voice their disapproval for regions if you are interested.

As much of a fan as i am of them, sometimes they are just waste.  Today i came across a code file that was had the following regions.  (I have changed the actual region tags to protect the innocent:)

#region Custom Methods

#region private bool MethodThatDoesSomething
private bool MethodThatDoesSomething(){}
#endregion

#region private void AnotherMethodThatDoesSomething
private void AnotherMethodThatDoesSomething(){}
#endregion

#endregion

By putting the private methods inside it OWN region region the author has accomplished NOTHING, except making my hunt even further for the code i need to modify.  This was just total waste.

If you are like me and like regions, my guess is that it is because you believe that it better organize your code.  But you must know when enough is enough.  Please don't put individual methods inside a region that is meant to ONLY hold that method.

Till next time,



Comments

Eber Irigoyen said:

it seems the developer thinks that only regions can be collapsed

# January 15, 2008 10:36 AM

Dave Schinkel said:

2 things

1) If you're gonna use regions, you better make sure your team has a standard on them.  I can't stand seeing different classes using a different structure

2) Keep them generic in nature.  Instead of lets say naming a region for example "Code to Bind Product GridView"...just name it "Binding Methods" or something

3) I agree, too many, just makes it more confusing.  Since this is OOP, regions can be divided based on areas such as Properties, Page Functions, etc.  Keep it simple such as "Constructors", "Utility Methods" etc. and use OOP terminology, not custom termonology

# January 15, 2008 11:50 AM

Dave S said:

Amazing, I've never seen something so stupidly coded:

#region private bool MethodThatDoesSomething

private bool MethodThatDoesSomething(){}

#endregion

that's just like creating a method that only does one thing, calls another method which is just dumb.  Obviously whoever did that don't know how to code in the first place.  Sad but just stating a truth.

# January 15, 2008 11:53 AM

DotNetKicks.com said:

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

# January 15, 2008 11:55 AM

Nikola Malovic said:

If separation of concerns is properly applied types in .NET are relatively speaking with small number of members/lines, so the value of regions is disputable in my opinion

I am using them ocassionally only to "group" (read hide) a bunch of helper private methods which were result of "Extract method" decomposing of complex methods.

Summarized: IMHO, one should avoid using regions and if using them at all, he should use them to hide code noise

# January 30, 2008 5:01 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Red-Gate!