Bob Parsons
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.



  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

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

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

I stumbled over this article after being totally fed up with the code I was working on. I actually read C# Regions Considered Harmful first and could not agree more with him. I actually turned outlining off a long time ago.

I ended up with the following approach; regions are only sensible to group the API of a class. This ensures that other developers keep the class clean. I think of regions analogous to doxygen's blocks. All other use should be banned. 1/21/2011 3:35 AM | Sean Farrell

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

What is wrong with C# developers?

A class is a set of fields and methods and should be considered as a whole. A class should have one purpose only, and should have only the fields and method necessary to fulfill the purpose.

Private fields, private methods, public methods, properties, ctors, etc. are all part of the class and serve this one purpose of a class. There is never a need to just see the parts of a class, because only with each part of a class can the class function. There is no need to put them in a region and hinder the readability of the class.

But if you have so many, for example, properties in the class that you need regions to group them, then you class is just too big. Instead of using a region to group the properties together, split the class in more classes. Again, a class should have one and only one purpose.

Why you need to group an API? You already have a language construct for that, it's called a CLASS. Again, a class should have only one purpose, one API, not a set of APIs.

Why you need regions to group code that is relevant? You already have such a language construct, it's called a CLASS. A class should have one purpose and it should contain only the methods to fulfill the one purpose.

Instead of using a region, why not using a class?

I know why, because it's much harder and it's so much easier to just put a region and hide your code. Hide that you have multiple APIs for one class (Sean Farrell), hide that you have methods that are not related to each other in one class and hide that you have 20 ctors and 100 properties all for just one class (blubber). 4/7/2011 12:42 PM | Erwin M.

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

i don't have a brain.So please Help me. 11/29/2011 9:27 PM | Dhaval Soni

Post a comment