I suggest you ...

#region Directive Considered Harmful (was "Get rid of #region")

My informal analysis concludes that #regions are used for evil 96% of the time.

430 votes
Sign in
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
K. Scott Allen shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

Hey Scott,

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


Sign in
Sign in with: facebook google
Signed in as (Sign out)
  • Paul Baxter commented  ·   ·  Flag as inappropriate

    As someone who has used c# for many years. I can say that I HATE them. They get in the way of reading code and clutter the code.

    By default they are all collapsed so to read code I must expand each one of them by hand. What frustration this causes.

    I used to use them when they first came out but I now consider them EVIL and I wont permit check in with them in the code

  • Maziar Rezaei commented  ·   ·  Flag as inappropriate

    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  ·   ·  Flag as inappropriate

    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.

  • Nicholas commented  ·   ·  Flag as inappropriate

    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  ·   ·  Flag as inappropriate

    Regions can be used to separate aspects of a code implementation or group Fields/Properties/Methods/Events separately

  • Matthew Martin commented  ·   ·  Flag as inappropriate

    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.

  • Yossu commented  ·   ·  Flag as inappropriate

    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!

  • John Brown commented  ·   ·  Flag as inappropriate

    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  ·   ·  Flag as inappropriate

    I personally dislike regions too. So I don't use them. Removing region directive would break many, many lines of existing code.

    Duff idea!

  • Anonymous commented  ·   ·  Flag as inappropriate

    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  ·   ·  Flag as inappropriate

    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  ·   ·  Flag as inappropriate

    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.

    Though, as much as I hate regions and prefer to write code I don't need to hide, to just rip it out now would be counter-productive. I'm a bit surprised, though, that we're now going to see regions in JavaScript and CSS. That's just a joke, I hope, as that'll force all devs to use VIsual Studio, when some might like a different tool for certain aspects of a project.

  • Vernon Burt commented  ·   ·  Flag as inappropriate

    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.

  • Ian commented  ·   ·  Flag as inappropriate

    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.

← Previous 1 3 4 5 6

Feedback and Knowledge Base