#region Directive Considered Harmful (was "Get rid of #region")
My informal analysis concludes that #regions are used for evil 96% of the time.
The #region directive encourages people to write classes that are far larger than they should be. It allows lazy developers to "hide" the cruft and navigate their code "geographically" rather than semantically.
They are a mess, and should be eradicated with extreme prejudice.
Thanks for taking the time to make this suggestion. I think every programmer has one or more features in any language which they just hate because they’ve been abused by someone else. I’ve renamed this post to capture that immortalized hatred all programmers share for such evil constructs!
Having said that, #regions do have valid use 4% of the time to someone (arguably grouping interface implementations within a class?) and even if there weren’t it’s really far too late to remove them (even if we thought they were evil, which we don’t). It simply wouldn’t be responsible to the millions of developers out there who might have #region directives in their code today either intentionally or having inherited them from other developers (at least some of which have passionately argued on this thread below) to break that code now without a pretty darn good reason. As a rule, generally speaking it’s somewhere the C# language design team just doesn’t go.
Luckily we have tools like StyleCop and one day “Roslyn” which can be leveraged to enforce our dreams of code purity, at least within our own spheres of influence.
-Anthony D. Green, Program Manager, Visual Basic & C# Languages Team
Maziar Rezaei commented
As a programmer who have coded with C# since it's beta release, I can say that the argument of regions "encourage complexity" are just like "I cannot see the forest for the trees". Use of regions, just like any other language feature, should be very delicately regulated. If you are using regions for separating methods based on their tasks and relations, then yes, they can definitely be harmful since many methods might not fall in only one region.
The way I primarily use them is to organize based on accessibility levels. That makes code much more clear as to which are public facing members; extremely important in framework or library development! Besides that, I don't think anyone likes a class to be a pile of mixed public, private and internal members of all kind. Region can enforce BASIC organization of members which is vital.
And let's be honest, we all have seen programmers that simply have no sense of "good" and "aweful". Like those who use auto generated xml docs with a spell checker just to ensure they don't have misspelled words in member names. How do you like removing XML docs now?
David Lean commented
I love regions. They are a very convenient way to get the code I'm not currently working on out of the way.
Similar to split screen. But persists until I'm done working on that feature & can "split" in multiple places.
Of the many ad hominem tantrums, one of the comments to the original post asserted: "...Regions are great for identifying junior developers who are otherwise referred to as senior..."
To write a thesis encouraging some uprise against a practice such as the oppression of clicking on a [+] REGION in the IDE seems a common practice among coders.
That one coder disapproves of # REGION is of no interest; although certainly if written with reason, I will entertain his/her argument yet unless I agree with the assertion, the persuasion will have no effect on me and I will continue with my personal coding preferences. As I commented earlier, some of (vocal) developer population seems to suffer histrionics and manufactures crusades for a cult of triviality in which converts are sought and disbelievers disparaged as heretical bad coders.
I watch these code cults come and go... the cults of SCRUM", "peer-programming", "extreme programming", "ORM everything", "IOC wrappers for all", "strict adherence to the GoF", "big data" etc... All the while my practice remains unchanged: code as I want according to the project needs and to my standards without concern for coders who would turn every personal preference into religious conquest.
I have my own code practices that I will not violate: avoiding "var" unless no other choice exists, prefixing my private variables with "__" and using "this." to separate method scope from class scope private members. Yet I make no religion of them.
- L. N. de Lioncourt
ClipFlair admin commented
Regions can be used to separate aspects of a code implementation or group Fields/Properties/Methods/Events separately
Matthew Martin commented
I wish there was an option to treat #region as a comment or as invisible text, that it was default. That some developers have switched from object oriented development to region oriented development (regions as class substitutes) shouldn't penalize everyone else.
With all due respect to the originator of this suggestion, I disagree very strongly with it.
Regions, like any feature can be abused, but that doesn't mean they are bad. When used correctly, they can be extremely helpful. Furthermore, they do not inherently cause any violation of SOLID principles. I have classes of 30-40 lines that use regions, and I still find them helpful.
If you don't like them, don't use them. Please don't suggest their removal based on your vague, unspecified "analysis" - which for all we know could have been five minutes daydreaming in the bathroom!
Michael Paterson commented
@MichaelJohn I think regions encourage organization.
Michael John commented
Regions encourage complexity
I'm afraid I strongly disagree with this idea - regions are good if use appropriately. If you don't like it you can disable outlinging : http://stackoverflow.com/questions/115694/how-to-permanently-disable-region-folding-in-visual-studio-2008
John Brown commented
No one is forcing you to use them.
It took me a while to realise how much tidier they can make my code and I now find them helpful.
As mentioned many times in earlier comments, removing them would break too much of the existing code base.
Dave Arkley commented
I personally dislike regions too. So I don't use them. Removing region directive would break many, many lines of existing code.
Wow, this is such an awful idea! The guy doesn't even bother posting anything about his so-called analysis.
Hey, I have a slightly less informal analysis that says 98% of #regions are super useful... and that at least 1 person named K. Scott may be out of his mind. :-)
Quick super useful example:
private object _someLock = new object();
private int var1;
private int var2;
private int var3;
Now from the name of the first variable you probably infer that it is used for thread synchronization. Cool. But which variables are to be locked by this lock, a new developer that just checked out the project code would wonder! Simple:
private object _someLock = new object();
#region _someLock's Domain
private int var1;
private int var2;
private int var3;
Now there you go! Now anyone knows for sure which variables are being synchronized with which lock!
Corey Murtagh commented
Next up, we're going to have to start a campaign to get rid of comments. After all, COMMENTS LIE! If you read a comment you might get a false impression of what the code ACTUALLY does based on what the comment is telling you! Comments let people write terrible code and expect that they can get away with it by sprinkling a few explanatory comments throughout! Without comments, people would HAVE to write better code, right?
And for that matter, NO CODE FOLDING AT ALL! No more collapsing of methods in the editor for ANYONE.
And we should all be writing code by redirecting echo statements and debugging with windbg connected remotely by serial port.
I'm sorry, but that's the level of stupidity I see represented in this type of frivolous request.
The overwhelming number of "don't remove regions" comments here are a perfect example of why down-voting should be allowed. This is a stupid suggestion and should be discarded out of hand. Regions are useful to those of us who use them, and irrelevant to those that don't.
Michael Sumerano commented
Anti-Region campaign: http://www.richard-banks.org/2011/02/anti-region-campaign.html
While you can argue either side, I have yet to see appropriate use of regions, even with "good developers." Regions are equivalent to comments. THEY LIE! To quote Robert C. Martin in Clean Code, "Nothing can be quite so damaging as an old crufty comment that propagates lies and misinformation."
AFAIK, regions were only added due to the fact of needing to hide generated code that could not be removed from classes early on in .NET's life. Once partial classes were added, they were no longer needed for this purpose, but plenty of developers latched onto what seemed like a great feature.
Vernon Burt commented
This is silly, #region is great for collapsing using statements and doesn't hurt SOLID principles by their existence - only by their abuse. Logically we'd have to ban keyboard input because it leads to spelling errors.
Getting rid/kept regions can do nothing with leading/violating SOLID.
I've mainly seen regions used as a way of making classes that are too large and ignore SOLID principles less unreadable. I'd be happy for them to be removed for this reason alone.
380 votes??? Why not get rid of the remarks ?
Akash Kava commented
Get better programmers in your company, we have good programmers and they use regions nicely. It is great feature and we wish it was there in every language.