Geeks With Blogs
Bob Parsons Do as I say, not do as I do

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.

Posted on Wednesday, August 27, 2008 11:14 PM | Back to top


Comments on this post: C# Regions... When to use them?

# re: C# Regions... When to use them?
Requesting Gravatar...
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.
Left by Mark Flory on Aug 28, 2008 8:54 AM

# re: C# Regions... When to use them?
Requesting Gravatar...
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
Left by rusty on Aug 28, 2008 7:34 PM

# re: C# Regions... When to use them?
Requesting Gravatar...
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!
Left by danielearwicker on Oct 14, 2008 5:35 AM

# re: C# Regions... When to use them?
Requesting Gravatar...
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.
Left by blubber on May 26, 2009 5:40 AM

# re: C# Regions... When to use them?
Requesting Gravatar...
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.
Left by Sean Farrell on Jan 21, 2011 3:35 AM

# re: C# Regions... When to use them?
Requesting Gravatar...
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).
Left by Erwin M. on Apr 07, 2011 12:42 PM

# re: C# Regions... When to use them?
Requesting Gravatar...
i don't have a brain.So please Help me.
Left by Dhaval Soni on Nov 29, 2011 9:27 PM

# re: C# Regions... When to use them?
Requesting Gravatar...
Regions should not be used for commenting. Ever. However, they are particularly useful when grouping code for collapsed outlining. For example, I recently wrote a class that had apx 40 internal vars plus related inteface, 6 ploymorphic initialization functions, and groups of related functions that served multiple areas of use (payment processing - direct, payment processing - via saved profile, profile management, etc) and all fit neatly and properly into one class that managed payments through a particular provider's gateway. By collapsing the various areas as needed, I was able to focus on the particulare aspect at hand.
Left by Jeff S. on Feb 27, 2012 7:38 AM

# re: C# Regions... When to use them?
Requesting Gravatar...
When C# code is converted to other language this regions used in C# is a problem.
Left by Joseph Paul on Jul 26, 2015 7:01 PM

# re: C# Regions... When to use them?
Requesting Gravatar...
I have one really great use for regions, which I am in fact living this very moment. I am in the process of refactoring a bug system that has numerous 500+ LOC methods, with low cohesion, SRP violation, reused temp variables, etc. In short, it contains all kinds of what everyone on this blog would unanimously agree are "code smells", regardless how overused you think the term is. The code also has no unit tests. Before refactoring, we need to create unit tests. I've put in regions to help identify blocks of code that determine unit test boundaries. After we are done with the unit tests, we will begin refactoring. As we refactor, the regions will be replaced with "extract class/method" and the regions will gradually disappear.
Left by Vance Duncan on Jun 09, 2016 6:58 AM

# re: C# Regions... When to use them?
Requesting Gravatar...
I just couldn't leave your website before telling you that I truly enjoyed the top rated high quality data you present for your visitors? Will be returning again frequently to check up on fresh article. obat miom tradisional
Left by Veherba Herbal on Jan 23, 2017 3:37 PM

# re: C# Regions... When to use them?
Requesting Gravatar...
Your publish had provided me with another point of view on this topic. I had no notion that things can work on this way as well. Thank you for sharing your opinion. obat jantung koroner tradisional
Left by Veherba Alami on Jan 23, 2017 3:38 PM

# re: C# Regions... When to use them?
Requesting Gravatar...
I would really like to appreciate your nicely written content their useful and your writing style helped me to that with absolutely no difficulty. Gratitude obat asam urat tradisional
Left by Veherba Obat Herbal on Jan 23, 2017 3:39 PM

Your comment:
 (will show your gravatar)


Copyright © Bob Parsons | Powered by: GeeksWithBlogs.net