C# Regions are not Useful? Give me a Break

Technorati Tags: ,,

I often see arguments about use of regions in C# code.  And this is one of the most annoying things I see on the Internet in my opinion when it pertains to code structure.

Whoever says regions are not to be used, I’d love to see your code and know what standards you have established as a team with your code base. I bet you it’s a mess and the reason you don’t like regions is because the developers on your team has abused use of them and that there is no logical pattern or team standard established.

I’ve been on plenty of teams who have used regions effectively and at a top level grouping that made classes and finding things much faster overall.  I definitely agree that unorganized use of regions and overuse should not be, but to say not to use regions at all is like saying put all of your damn html on one line and don’t indent…another thing I can’t read.

If you have a team who can’t use regions effectively it’s either because of one or more of these three issues:

  • They don’t care about team standards
  • There is no pattern in place and standard that expected of the team
  • There are no code reviews to help keep that standard and pattern in place

There is absolutely no excuse to say that regions are never helpful. It’s part of development, part your team standards/patterns people and then code reviews can help to reinforce it. It’s called caring about code structure which goes along with name spacing, file structure of you project, and pattern structure of your code. To single out regions is foolish and only means you can’t figure out a standard practice on your team and template to force and use that is top level..simple as this:

#region Methods
#region Properties
#region CRUDs

…whatever

Who wants to scroll down a class all the time when you can go to a region quickly to find what you want as a starting point. And your classes should never be very long (SOLID) but still, regions do help. If Code Generation did not use regions or non-generated code did not, can you imagine? Granted I am not using code generated code now but was and it was nice.

I always use regions and always will, they are not chaos in my application so they are extremely useful when not used like a code & run team who has no clue of standards or patterns.  The argument not to use them should never come into play.  If you see a bunch of regions and they are not named well, talk to the team and have a team meeting about it and come up with a template that works. 

But never say they are ineffective and never to use them.  Give me a break.  You can say that about any aspect of code that is not organized and has no pattern or standard!  Every developer is going to have to change code, and maybe regions will change but if used correctly not that often.  That’s why it’s nice to use tools such as ReSharper to create and manage them as well.  Having no regions with this type of “excuse” is just plain ignorance.


Print | posted on Sunday, April 19, 2009 10:05 PM

Comments on this post

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
just a comment. the reason people put all HTML on one line is for size and speed. Expresion Web actually has the feature to deploy your site all on one line. That way you can edit in readable form and deploy for performance.
Left by Jonathan on Apr 20, 2009 10:20 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
Jonathan,

You're talking about file compression. That's not what I mean by HTML on one line. If was not clear my apologies but I am talking strictly about lack of tabbing and indentation in mark-up leaving it hard to read. That's laziness.
Left by Dave Schinkel on Apr 20, 2009 11:29 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
finding things much faster overall.


I always though CTRL-SHIFT-F works quite nicely.

Left by mycall on Apr 21, 2009 12:23 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
What I lacked most of all when I had to write a project in Java, was the lack of regions...

BTW, I wonder how big are the projects which those guys who so fiercely critcise regions are actully working on. I wouldn't be surprised if those were smaller and short-term projects (no more 150 000 lines of code)
Left by xxjthxx on Apr 21, 2009 12:37 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
ctrl-i is the king of find in file. Also that drop down at the top of the page listing all methods and properties is pretty useful(if you use vs IDE or monodevelop). I think the issue here is using region to hide problem code. Why have the region CRUD in with methods and properties for example? it feels like crud is data access and may deserve its own class, rather be a hidden stowaway on this class. People also tend to use regions to hide overlong methods or over large classes. Perhaps this code needs refactoring but to the casual observer the problem is again, hidden. To some of us it just seems that region is more often than not being used to hide / excuse sloppy programming than enhance clean programming. (And if your programming is that clean region starts to look out of place anyway adding clutter to your zen :P) Anyway its just an opinion and this whole topic reeks of religion :D Just be happy using what you use AS LONG AS YOURE ALL USING C# ON AN MS PLATFORM
Left by Pete on Apr 21, 2009 6:03 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
CRUD can be held in any class. For example, you have Customer and it's going to have CRUD methods. You have Employee, it's gonna have CRUD methods. It's a section you'll see in every business object in your DL.
Left by Dave Schinkel on Apr 21, 2009 7:07 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
Using or not using 'region' is a habit, it can be a personal one, or it can be a project one. For people who are not used to 'region', they normally have very good tools to help them organize and find things. (Personal observation: they tend to be more experienced agile guy that they invest money in tooling) Asking them to do 'region' is adding more burden to their productivity.

For people whole are used to 'region' and lacking proper tools. Make region a team habit/standard can help ...

For me, I would rather use tool then spending time in moving things around ...
Left by Harry on Apr 21, 2009 7:53 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
Harry,

I do not buy that argument. Tools are not a replacement for good code structure. Furthermore, what you code and how as it related to code structure affects the entire team. So just because you don't need regions is not a an excuse or expectation for the entire team to work YOUR way only.

And to state that it's more work to add regions is like saying it's more work to implement design patterns. Sure it's a bit more work and the two go hand in hand. But to say that, then why have any code structure at all is what your telling me. Adding regions should be habit and is not any more work than anything else we do in code...regions are simple and quik to add to any class and if you want to talk about tools, you can use many including ReSharper, VS templates, and more to automate creation of regions in your classes.
Left by Dave Schinkel on Apr 21, 2009 8:14 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
I was going to write a balanced, thought out reply where I was going to talk about merits of both sides of the argument. But what the heck lets just use the same tone the blog post above uses.

Regions are a solution to a problem you shouldnt have in the first place. Regions make files with large classes more readable. Any coding standard actually worth the bytes it occupies on your hard drive should tell you something about single responsibility principle. Adhering to this will make sure you never get classes that are so bloated you actually need something like regions.

Regions hide code. Code that should be hidden for a particular context should be in a different class than the one you're reading because clearly it is not relevant for the responsibility that class is written for. Regions are just a band-aid to hide badly partitioned code.
Left by Mendelt Siebenga on Apr 21, 2009 8:59 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
Mendelt,

How did I know this would come up. Even if using that pattern, you STILL should use regions. Just because your class has minimal # of members doesn't mean regions are not useful. Use your ReSharper or VS keyboard shortcuts to expand and collapse them then. Big deal.
Left by Dave Schinkel on Apr 21, 2009 9:22 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
Here's my counter-post:

Title: "All you #region using-crackheads are retarded"
Body: Actually I didn't mean to call anyone crackheads, and I'm not saying anyone's retarded, but in most situations, #regions are abused and are indicative of larger problems. I think there are some scenarios where well-reasoned usage of #regions is acceptable, so they're not entirely bad. Thank you for your time, and have an excellent weekend.
Left by Peter on Apr 21, 2009 11:56 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
Regions are pointless and bloat up files for really, no reason. Just use CTRL M+O to compress your methods. If it's not evident what your method is doing by reading just the name of it, your method names aren't good. Also, are you telling me you need to read the region description to tell if something is a property rather than just glance at it?

Sorry for the somewhat hostile tone, but as other have mentioned, that's your tone...so I'm figuring you can't really fault me for mine.

Cheers.

Left by Sanjay on Apr 21, 2009 12:46 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
>>>n most situations, #regions are abused and are indicative of larger problems

that is...again IF you abuse them. My tone is to the point, making my points nothing more. It's called an opinion and backing up your points...that's it people! You're reading too much into tone.
Left by Dave Schinkel on Apr 21, 2009 1:32 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...

My problems with regions.

1) for concise well maintained classes.
Its a waste of time to have them because every time you open the file you have to expand the regions to seen the properties, methods (in the collapsed view). I don't want to expand everything. Just those regions that are hiding code. For small classes I really feel like throwing a shovel at the person who insist on putting there set of regions in every class.

2) for larger legacy classes.
Sames as above, expect you also get a nasty surprise that someone has hidden under the regions. + people are more likely to ignore code hidden in regions.

Can I say "hidden in regions" once more..
Left by Mark on Apr 21, 2009 6:49 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
Mark,

I'm not going to say people aren't entitled to an opinion but all I hear is excuses. Use a shortcut toggle to expand/collapse. And if a developer is too lazy to expand and see what's in a class, they should not be a developer.
Left by Dave Schinkel on Apr 21, 2009 7:49 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
Also may I say that tone = opinion. Like it nor not. There is no requirement here to be "polite" when expressing an opinion. I get to the point and I DO think out what I write. The difference is in how hard you take it...it's an opinion and I'm entitled to it even if I put it bluntly. In fact, I do not think my post is so bad.
Left by Dave Schinkel on Apr 21, 2009 7:51 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...

Yes I am lazy.. I do just enough to get the job done and I make it as simple as possible.

I don't go making my classes look pretty/neat with regions, that actually tell you nothing, hide the code, and waste my time.
Left by Mark on Apr 21, 2009 9:35 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
Regions were a hack to get around the lack of partial classes in early .net. And I admit that I used them from time to time.

Then I learned SOLID and design patterns, which taught me how to actually design and architect my code.

Now, regions are a code smell. They are a clear sign that too much is going on in one class.
Left by Chris Brandsma on Apr 21, 2009 10:45 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
>>I don't go making my classes look pretty/neat with regions

we could go on and on about this but I don't see why there are such cocky comments being made. It's not about pretty, it's about code that is organized outside of just method names, class length, etc.
Left by Dave Schinkel on Apr 21, 2009 10:59 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
>>Now, regions are a code smell

I think code smell is too much of a relative term.

And I think just because you are SOLID doesn't make a replacement not to use regions.
Left by Dave Schinkel on Apr 21, 2009 11:00 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
All this talk about SOLID does not make excuses not to use basic thing such as regions. In fact a SOLID class may have a ton of properties. Who wants to scroll all the way down to find methods. Some classes can have 20-30 properties depending on its function...even IF it is a SOLID CLASS.

I would love to see just how much SOLID everyone's classes are here. I'm sure not all of them are perfect and SOLID is still something people are trying to master and solidify. It's not black and white people. Sure, it is in definition but actually practicing SOLID is not something each one of us is perfect at. And again, this is no reason to say regions are not useful.
Left by Dave Schinkel on Apr 21, 2009 11:07 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
CTRL M - O people.
Left by Dave Schinkel on Apr 21, 2009 11:12 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
Regions are a tool like anything else. They are what you make of them.

Hiding a giant hack behind a region, yeah, definitely a code smell. Partitioning your class into private fields, private methods, public methods, or whatever categories you choose and keeping each category clean and organized is a perfectly fine use of regions that has its advantages.
Left by Matt on Apr 21, 2009 11:14 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
>>>Partitioning your class into private fields, private methods, public methods, or whatever categories you choose and keeping each category clean and organized is a perfectly fine use of regions that has its advantages

Thank you. I'm NOT talking about code smell in terms of a messy class. I DO support SOLID in fact I have a post on some aspects of SOLID so it's not that I do believe that a class should be short. I just don't think SOLID is an excuse not to use regions, plain and simple. There's no dancing around my position and why. Many assumptions have been made around what I believe and I've stated what I believe and why.
Left by Dave Schinkel on Apr 21, 2009 11:17 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
Dogmatic tripe. This is a religious issue, and folks on both sides are guilty of making claims that are simply not justifiable, in an attempt to make the choice anything other than a preference. In this case, Dave's argument is boiled down to his bulleted list of reasons why someone might choose not to use regions.

•They don’t care about team standards
•There is no pattern in place and standard that expected of the team
•There are no code reviews to help keep that standard and pattern in place

I call BS. Team standards may not require, and in fact may prohibit, usage of regions. If that's the case, and I care about team standards, I won't use regions. You don't need regions to establish a pattern for code structure. Take any one of Dave's source files with regions in place and remove those regions and you'll still have the exact same pattern for code structure. A code review doesn't need regions to enforce any structural patterns, and in fact automated tools such as StyleCop are probably better for this anyway. So, none of his suppositions hold up, rendering the entire post nothing more than a public tantrum. A fairly offensive one at that.

Regions really only give you two things: the ability to collapse code, and the ability to search for a region name. The benefit of collapsing code is debatable, and the consequences of having this ability (collapsed by default, and search behavior when regions are collapsed) is one of the reasons some people dislike regions. This makes that feature highly debatable, with some folks loving it, others hating it. As for searching... the detractors correctly point out that in modern IDEs there are much better ways to navigate code, and that if you really want to be able to navigate in this way a comment suffices just as well.

So, do you use spaces or tabs?
Left by wekempf on Apr 23, 2009 7:53 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
Wekempf, would you like a tissue?
Left by Dave Schinkel on Apr 23, 2009 8:19 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
i love regions! i've always been using them, and my code is always clean, well organized, and i can always find things quickly.. and quicker and quicker!!
regions are fundamental, i can't imagine my code without them
Left by arwen_h on Apr 23, 2009 10:57 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
>>The benefit of collapsing code is debatable

sure, if your code is used for "hello world".
if you have >1000 lines, collapsing is necessary to give you a "look" at the code..
they are also useful for my students, sometimes i give them my code and i say "modify the equation X while doing Y in the code" and they start to become pale, then i suggest them just to look at the region doing Y and they feel better. i'm also sure that they wont make much damage outside that region.. eheheheh
(remark they are MS students... )
Left by arwen_h on Apr 23, 2009 11:03 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
@Dave Schinkel, just like the original posting, you show your maturity with your response. You wonder why people take issue with your tone? Because you're not stating an opinion, you're attacking those who disagree with you, making you no more than a troll.

@anwen_h, You're not much better, with the snipe "sure, if you're code is used for 'hello world'". Glad you have an opinion here, though it's important to realize it's really a preference. However, attacking those who disagree with you is childish.

More importantly, you're reasoning is severely flawed. Unless that region is inside a function with >1000 lines, you already have the ability to collapse your code (methods and nearly every other block is collapsible). If it's the method that's that large... you have bigger problems.
Left by wekempf on Apr 23, 2009 11:59 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
wekempf, all you do when you come to my blog is come with a very passive aggressive tone yourself. So don't pitch to me about immature. If you can't hack an opinion without being a complete ass who tries to only cut people down when you reply, simply go away then.  You can have your counter point when you reply but you went over the edge saying that my post is insulting.  To who??  you?  I don't care.  It's an opinion.
Left by Dave Schinkel on Apr 23, 2009 12:09 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
It's obviously insulted a lot of folks, based on the responses. I'm not being passive aggressive, I'm being polite... but screw that. You're an ass. No more responses are necessary at this point, and I'll move on and stop feeding the troll.
Left by wekempf on Apr 23, 2009 2:27 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
>>>It's obviously insulted a lot of folks

Dude, I'm not going to sit here all day and am definitely done quibbling with you. You feed off of intimidating and egoistic type of remarks to others. I did not pose anything rude to anyone. If you cannot take an opinion then do not come to my blog buddy. Simple as that. You can be arrogant and cocky but then you tell me I am when I'm stating a strong opinion. Again, go buy some Kleenex and find some other blog to bitch about how strong their opinion is.

If you cannot engage in a constructive dialog, go somewhere else.
Left by Dave Schinkel on Apr 23, 2009 2:59 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
@Dave: I doubt you insulted anyone. But you did strike a nerve. :) You may get flames, but that is a good thing. Same thing happens to me when I tell people that Stored Procedures are the wrong way to go.

About the SOLID part (and TDD for that part): something I strive for is smaller, more tightly defined classes. If my class is only 200 lines of code, then I don't need regions. In fact, having regions would just get in the way.

Typically a large class for me is 1000 lines (usually a View -- UI form). That is achieved via a layered architecture. I don't have my CRUD code mixed in with my view code. And my business logic is in a service.

Now my other "trick" is ReSharper. Ctrl-Shift-G (Navigate from here), Ctrl-F12 (go to method), and Ctrl-N (go to class) and I can quickly navigate to anywhere in my application. Again, regions would be inefficient for me.
Left by Chris Brandsma on Apr 26, 2009 9:56 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
I think most people who do not use regions either never had to implement anything complex and/or they are very lazy (or they do have a very good tool that automatically structures the code for them...but that doesn't count for me because most people don't have such a tool).
The "hiding bad code" argument is some kind of stupid, all things can be ill-used but it is in your hand to use it in a good way. If you are a good coder and have structured your code well you can put it into regions without much effort and it makes it easier to navigate to the right function/property. Opening and closing regions is bothering you? Phew if you prefer searching your numerous functions/properties...and especially when you don't remember the exact name of the function...but each to his own, have fun not using regions. I wonder if people not using regions do open all cupboards in their room when they want to find a pair of new socks...
Left by sth_Weird on May 06, 2009 10:23 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
hi admin, you seem very familiar about this, nice discussions and keep up the good work.
thanks
Left by tukang nggame on Jun 18, 2009 12:53 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
I really don't like regions because they hide the code, and make it harder, in my opinion to see what's really going on in a file.

I DO however use several macros that format and alphabetize the objects ( Props, methods, attributes, etc ) within the code.
Left by Michael on Jun 19, 2009 2:10 PM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
Browsing a piece of code (with or without Regions) is like finding stuff on the internet using an index (e.g. http://www.google.com/Top/Computers/" title="http://www.google.com/Top/Computers/">http://www.google.com/Top/Computers/). Using a simple search tool (e.g. http://www.google.com) is much smarter.

ReSharper‘s “Go to File Member” or “Go to Symbol” are examples of great search tools for Visual Studio.

However I will admit that Regions are nice if you want to print out your code and read it on plain old paper. But that is so 80ies.

:-)

Left by Mikael on Jul 23, 2009 3:38 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
@Mikael

Outside of this continous argument about SOLID and having short classes...I get that. But even with a short class, regions are fine. I think that people who say regions are clutter just don't know how to:

a) tolerate organization
b) search smartly and use smart shortcuts to get around them

they have never been "in my way" and in fact I invite them when used smartly and via a team standard in terms of template for regions. I cannot imagine for example outside of your own custom code having no regions in generated code such as CodeSmith, etc.

Also I'd like to know what defines a "short" class. I've seen classes that are pretty long because they have a lot of necessary properties only due to the nature of that object. That makes the class long and is still within SOLID principles. A class can be pretty long just by the nature of its basic members without being a class that is "a mess" or a class that is "doing something outside its means".
Left by Dave Schinkel on Jul 23, 2009 8:48 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
I still say Regions are not useful but there was quite a bit of uproar regarding my rant about C# regions. At the time, I was looking through a lot of really bad code, and I was pretty peeved at how all the pieces of complex code were hidden away by Visual Studio. However, some of the comments seemed to imply that there is no such thing as “perfect” or “good” code when you refer to code written by someone other than yourself. And, to be honest, that is true: I like to think the code I write is better than most other developers.

220-602 practice test | 640-822 practice test | 350-030 practice test
Left by blythet on Sep 11, 2009 1:27 AM

# re: C# Regions are not Useful? Give me a Break

Requesting Gravatar...
I would like to inquire if it is possible to obfusicate #regions in a c# script
Left by Gary on Sep 19, 2009 7:29 AM

Your comment:

 (will show your gravatar)
 
Please add 6 and 5 and type the answer here: