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 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
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Francis Rodgers commented  ·   ·  Flag as inappropriate

        I would agree with getting rid of # regions as long as we are given a clean alternative to grouping our code. Why not just use open and close curley braces anywhere, and then you can collapse the entire grouping. i.e.

        public class MyClass()
        {//Open class

        {//Open grouping
        private void MyMethod1 ()
        {
        //Some code
        }

        private void MyMethod2 ()
        {
        //Some code
        }

        }//Close grouping

        private void MyMethod3 ()
        {
        //Some code
        }
        }//Close class

        I do agree that regions are far to messy, but something like the above would be great Especially if you could add a comment right beside it to show as a title for the region when collapsed. For example the above would like like the below when collapsed:

        public class MyClass()
        {//Open class

        {Open grouping

        private void MyMethod3 ()
        {
        //Some code
        }
        }//Close class

      • Mark Hurd commented  ·   ·  Flag as inappropriate

        This suggestion having 300 votes really suggests uservoice is not a useful interface!

      • abatishchev commented  ·   ·  Flag as inappropriate

        Why, WHY on Earth this ticket is not rejected and CLOSED yet?! To make yourself more notorious, Microsoft please switch the status to Under Review, LOL.

      • Vadim Milichev commented  ·   ·  Flag as inappropriate

        Why ban hammers if you hit your thumb?! ;)

        Install JsEnhancements to get regions in .css and .js files!

        Leave regions alive!

      • David Blaine Fullerton commented  ·   ·  Flag as inappropriate

        That's a terrible idea. They're very useful for hiding underlying details. I like skimming once the code is trusted and hiding unnecessary details.

      • drweb86 commented  ·   ·  Flag as inappropriate

        That's worst idea. Instead of removing tool that is used in bad way, teach your staff with guidelines to use in good way.

      • Corby commented  ·   ·  Flag as inappropriate

        This is the worst idea I have ever heard of.

        My package Regionizer at http://regionizer.codeplex.com Regionizes code into regions for private variables, events, constructors, methods, and properties.

        All of the methods, events and properties are sorted in alphabetical order, to me coding without Regions is spaghatti code.\

        My package (which docks in the same location as the solution explorer or properties window) formats your code consistently so no matter what coding styles each develop has the code is always formatted in the same manner.

      • abatishchev commented  ·   ·  Flag as inappropriate

        Please decline this ravings as soon as possible.
        What for do you keep it here?
        To review in future? God, please no.
        To open a discussion? I doubt that's the appropriate place for that.

      • Anonymous commented  ·   ·  Flag as inappropriate

        Why on earth would an individal presume that his opion on this should become global. Most of us like to use some organization in our code. If you don't like #regions, don't use them! Leave the rest of us alone.

      • che commented  ·   ·  Flag as inappropriate

        I LOVE #region. It really helps me organize my code. I don't see what the problem is.

      • Dawid Starkowski commented  ·   ·  Flag as inappropriate

        I don't get the problem. If regions are misused in your team, just forbid them. Simple.

        I use them right and I want them there.

      Feedback and Knowledge Base