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
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    K. Scott AllenK. Scott Allen shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →
    David MaysDavid Mays shared a merged idea: Eliminate the #region directive.  ·   · 
    declined  ·  Visual Studio TeamAdminVisual Studio Team (Product Team, Microsoft) responded  · 

    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.

    Regards,

    -Anthony D. Green, Program Manager, Visual Basic & C# Languages Team

    108 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • ClipFlair adminClipFlair admin commented  ·   ·  Flag as inappropriate

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

      • Matthew MartinMatthew 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.

      • YossuYossu 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 BrownJohn 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 ArkleyDave 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;
        #endregion
        private int var3;

        Now there you go! Now anyone knows for sure which variables are being synchronized with which lock!

      • Corey MurtaghCorey 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 SumeranoMichael 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 BurtVernon 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.

      • IanIan 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.

      • Akash KavaAkash Kava commented  ·   ·  Flag as inappropriate

        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.

      • Corey MurtaghCorey Murtagh commented  ·   ·  Flag as inappropriate

        This is a completely frivolous request that has nothing to do with C# as a language.

        #region is a purely decorative directive that is used only to control folding within the development environment. Removing all #region and #endregion directives in a source file will not change the generated code, except as far as changing the line numbers in debug information.

        How developers choose to use regions is up to them, and has nothing to do with the language itself.

      • pvittpvitt commented  ·   ·  Flag as inappropriate

        When regions are misused, don't eliminate regions. Instead empowering your develpers to use them the right way seems to be the more constructive solution.

      ← Previous 1 3 4 5 6

      Feedback and Knowledge Base