#region Directive Considered Harmful (was "Get rid of #region")
My informal analysis concludes that #regions are used for evil 96% of the time.
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
Michel Pavan Macedo commented
Finally someone with a great solution (@dandraka), but I don't get the X number of lines thing...
I guess a warning plus rules for gendarme, fxcop and stylecop would do the trick.
Harri Koppel commented
Totally agree. People keep putting them back after all the trouble I've been through deleting them.
As other have mentioned, #regions are a code smell. I can't imagine MS removing a feature that so many users are using though !
I would suggest that any #region with more than X number of lines could generate a #warning (other suggestions welcome).
ack! I 'need' #regions! please don't remove them :)
@matt65: Hiding is just a feature of #region, much like you can collapse a method or a property, But mostly I think it's about organizing your code.
If you feel the need to hide parts of your class, then you need to simplify your class.
#region are good to hide private stuff. In fact, what would be nice would be that the IDE would be able to add event hadler in a region (either with a specific name or in an existing region that already has event handlers (last one).
@Mads Kristensen That's terrific news, we're really looking forward to it! :)
It should be kept.
I don't know how many evil people you met lately, but definetly they are not around me firing their #regions on my code.
I try not to be evil nevertheless I make extensive use of #region, beleiving that it clarifies my code, please do not remove it.
Shiv Kumar commented
Scott, don't use them if you don't want. Just don't dictate how others should use their tools.
Tom Kerkhove commented
Keep it, it's good for structuring your code
Sergey Galich commented
#region is great to mark autogenerated code. i say keep it.
Morten Nielsen commented
I don't get how this is "evil". If you don't like it and it doesn't fit your coding style, well don't use it, but don't ask to remove a feature that other people use and love, just because YOU feel it's wrong (not to mention removing it would be a breaking change, which alone would mean this feature request would never go through).
Mads Kristensen commented
In VS11, regions are supported in CSS (CTRL+K,S) already and are coming to JS.
@JarretV: I don't think people "need" to use regions, but those who do use them, probably just wants to. I can't really see how SRP applies to the use of regions.
Building a module with a single responsibility does not mean that you cannot use regions. I mean, the module can still have properties, fields, public/private methods etc. that someone would like to use regions to organize the appearance of the code and still have a single responsibility.
I also agree they are evil. If your code needs regions then you are coding wrong. Please see the SRP http://en.wikipedia.org/wiki/Single_responsibility_principle
If you don't like it, don't use it.
There are lots of ways they could implement this-- make them open by default and non-collapsable by default, a button to globally replace them, a compiler warning or better yet a compiler error.
The worst crime of #region is that is is there and collapses by default instead of the tiny percent of people that like #regions having to enable it.