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

    Regards,

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

    105 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      Signed in as (Sign out)
      Submitting...
      • Akash 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 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.

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

      • Vince Panuccio commented  ·   ·  Flag as inappropriate

        The problem with regions is that they are named in a way that makes sense to the developer who initially wrote the code. I often come across regions like "Common methods" or "Helper functions". It's bloody madness and when you confront them, they look at you like it should be obvious.

        It's hard to find and read code with regions.

        The only regions I prefer are ones that make sense. "Constructors" is a good one, that's obvious. Or "Static Properties" is another.

        If you don't like regions, use CTRL M + CTRL L to expand everything. If you're that against them, have a commit hook in your SCM that disallows them.

        Now can we please stop complaining over stupid requests like this?

      • Ken commented  ·   ·  Flag as inappropriate

        My use of words doesn't mean that I've flagged this suggestion, but it's very inappropriate!
        #regions can be useful, and you did not specify any concrete reason to why it should be removed.

        All you can say is "I'm not going to use #regions!" and we'll respond with "Good for you!".
        The ones that do enjoy them shouldn't have to be condemned to live on without them only because you don't like it.

      • Dajo commented  ·   ·  Flag as inappropriate

        Finally found a beneficial use for regions. I now try to group class member together based on whether they best fit a creational, structural or behavioral concern. Basically the 3 categories identified in GoF patterns. It a usual way of highlighting methods that have more than a single concern and might benefit from a little refactoriing.

        So whilst I share your pain when inheriting other people's code they do now serve a useful purpose for me.

      • JMCF125 commented  ·   ·  Flag as inappropriate

        I think my last comment was too short. I can't believe this has 323 votes. Do you want to prohibit cars as well because crazy people may intencionally crash them?

      • JMCF125 commented  ·   ·  Flag as inappropriate

        #region is only a problem if you make it so. Used appropriately, they can be a great help. Too bad we can't vote down.

      • abatishchev commented  ·   ·  Flag as inappropriate

        Why don't nest regions? It's absolutely legal. For instance, "Inner classes" then "Properties" in each,
        Don't use regions inside methods' body, that what I definitely support. Refactor and extract instead!

      • Andrew commented  ·   ·  Flag as inappropriate

        "My informal analysis concludes that #regions are used for evil 96%"
        -- Then you don't know how to use them...

        1) Don't nest reigns.
        2) Have a clear understood usage pattern for you projects.

        I only use regions in my projects for grouping the following categories in order from top to bottom in a class object - "Properties, Constructors, Operators and Methods"
        Or I use regions for other very special cases. They are misused yes, but only by unorganized coders O_o

      • Brett commented  ·   ·  Flag as inappropriate

        #Region is not a problem, it is the way developers are educated to use them. When used to break up and clarify areas of work they are fine. I use them in MVVM Vm's to isolate Commands, Command Handlers, Bound Properties etc. When regions are called Public Methods, Private Methods is when they add no value to the code. Use seperation of concerns effectively and don't copy what you saw in some text books, and above all Think before you Region, if it add no clarity do not use it.

      • John Cronan commented  ·   ·  Flag as inappropriate

        Having eradicated smallpox, humanity's next major targets should be malaria and regions.

        A once-great idea with which the developer community clearly cannot be trusted...

      • drweb86 commented  ·   ·  Flag as inappropriate

        @Francis Rodgers: #region helps you distinguish betwean code and non-code. and it allows a meaningfull title instead of your approach.

      Feedback and Knowledge Base