Paul Trippett
Do as I say, not do as I do

C# Regions... When to use them?

Wednesday, August 27, 2008 11:14 PM

After reading the comments on my previous post mentioning I liked C# regions and got the general concensus is that these are very bad and evil inventions. I typed "C# Regions" into google and sure enough the first result was titled "C# Regions Considered Harmful" So my next question was why can this little handy things be considered harmful.

I have come to a conclusion and answer is over use. A lingering voice tells me this might open a horrible debate about whether they should or should not be used. End of story is that they exist we have to live with them so heres when I think they should and should not be used.

  1. A Region should probably never be used inside a function or contain just one function - A simple comment or <summary> block will suffice. If your function seems too big maybe you should consider refactoring. A region could make sense around an interface implementation *if* your class implements more than one interface.
  2. A Class should probably never contain just one region - Otherwise the region is simply the class (maybe you already have a comment or <summary> block on the class) so thats just duplicating information.
  3. Place a region around your private variable declarations - this is down to your programming style, I place all my private declarations at the top of the class and 99% of the time when i open a file I am modifying a function or property so there is relly no need to see them unless I am creating something new so, for me, it makes sense to place these in a region.

Theres probably a few more maybe this list will grow over time. I am not saying use them in this way, but more if your going to use them think twice and use them wisely.


Feedback

# re: C# Regions... When to use them?

Anything that is used, or especially over-used, can be harmful.

At my last job we had a web service that the developer had put a #region around every single method. That was insanity and made the code a pain to work with.

My original thoughts on regions is that they should be set around code that is relevant to each other. In something like a WS that would make sense. But now I am thinking with good design generally all code in a class should be relevant to each other. So then it makes sense for regions to wall off things I don't care about, like your private members variables example.
8/28/2008 8:54 AM | Mark Flory

# re: C# Regions... When to use them?

At one time our team went nuts about regions (it happened to be VB.NET) for everything. After awhile of using them with our maintenance hats on, we realized how foolish we were.

Personally, I do not use regions at all. When I had DevExpress Refactor! installed, I sparingly used them because you could right-click and move methods to pre-defined regions. There also was an option to alphabetize within the methods within the region.

rusty 8/28/2008 7:34 PM | rusty

# re: C# Regions... When to use them?

I think the majority of uses of regions is inappropriate, and in fact many are attempts to solve problems that are better solved by a tool rather than continuous manual effort.

So last weekend I wrote a Visual Studio 2008 add-in to fill that need: http://www.codeplex.com/ora - it's free, open source and easy to install or remove. Please check it out! 10/14/2008 5:35 AM | danielearwicker

# re: C# Regions... When to use them?

You should not need a (maybe even expensive) tool to make your code easier to read. If you do it's as smelly as those of the people who ill-use regions to hide smelly code.
I use regions. Even in a smaller file. I group variable declarations, constructors, properties, methods, interface methods, overriden methods. They make it easier to find things without the need of an extra tool. It's much easier adding a new property etc. because you can simply jump to that region. My collegues find my code much easier to read and better structured than those of people who do not use them. Ill-using them...well that's a matter of self-disciplin. If you fear that you'll produce worse code if you use them, then don't. I have yet to see a programmer whose code went worse by using them, or better by not using them. The only experience I made is that when working on a team, the code of people who used regions was much easier to understand and much tidier than the code of people who claimed regions were hiding bad code. 5/26/2009 5:40 AM | blubber

Post a comment