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...
      • CSharpJohn commented  ·   ·  Flag as inappropriate

        Real-life coders need #region, period. Learn how to use outlining functions.

        This said, MS should give us automatic code folding / reorganization

        - command to switch between two views of the code:
        (1) as written (e.g. grouped by related functionality)
        (2) sorted and grouped by type (fields / properties / constructors / methods...)

        - migration from #region to partial classes for grown-too-long classes

      • Kiran Chand Palakkattiri commented  ·   ·  Flag as inappropriate

        I think it is very important to have the region tag code organization, especially in multi-targeted code or segregating the functionalities with in a large function.

        Anything good can be used for bad as well; possibly, lets get rid of the bad by providing guidance on best practices of using it; but lets not get rid of it as a whole :)

      • Trisped commented  ·   ·  Flag as inappropriate

        #region is an organization tool. I find it very helpful on team projects or even smaller projects as it dictates where methods and properties should be placed, making it easier to find them latter.

        I use them in classes with 8-12 elements (including constructors, properties, class variables, and methods). Most of the time I only need information from one or two regions, which may not be adjacent. Using regions I can collapse the code I do not need so I can see the code I do need easily.

        I am not a fan of large or ultra large classes, but placing properties, internal methods, event handlers, and local variables in their own classes just to maintain order only adds more problems and limitations. What if a property needs access to an internal method? Do you force the properties to depend on the internal methods class, so they cannot access the properties? Or do you code the method twice? Or do you make a new class to hold the properties which require the internal methods? Sure, you could define an interface, but then you are adding hundreds of lines of code, and much more complexity, just to save yourself from needing to use a simple and helpful organizational tool.

        This is a bad idea. If you do not like #regions then do not use them.

      • peterchen commented  ·   ·  Flag as inappropriate

        If the problem is "to complex code that needs to be refactored", removing regions won't help.

        All arguments I found mix confuse interface complexity (which should be low) with implementation complexity (which should be higher.)

        Finally, if #regions *are* a code smell for code to be refactored, removing them is equivalent to pinching your nose.

      • Levi Page commented  ·   ·  Flag as inappropriate

        Maybe we should remove select case too since it's often misused. Too bad there isn't an option to vote on the dumbest idea.

      • Kikoz commented  ·   ·  Flag as inappropriate

        Unbelievable! Those of you who hate #regions most likely also hate when jehovah witnesses knock on your door. You sure tell them "Please keep your God to yourselves, let me believe in what I believe". And yet, you ask MS to enforce your personal belief on the rest of us. Don't like #regions? Don't use them!! Found a method and can't figure out where it is declared? Simply right-click it and select "Go to definition..." Or click its name and hit Ctrl-F3. And I'll continue to surround all those private helpers with regions in order to enjoy nice and clean navigation, thank you very much.

      • Andi commented  ·   ·  Flag as inappropriate

        #regions are evil, that is a fact. If you start thinking: "so, i am finshed with my monster class, lets see how i can hide the code with regions" , bang!

        People try to hide complexity with regions. I often ask in peer reviews: "Were is that method coming from you call there? Oh, it is a helper method in the same class. I moved all the helpers into the helper region, wait i will search for it"

      • Bobby Dranzek,KG4QGK commented  ·   ·  Flag as inappropriate

        hmm ms could change it into regional lockout / codeing like ntsc-j and ntsc-u/c like on the good old large rectangle playstation / playstation2 and yes i carry a ps ps2 and ps3 sdk liscenses i would also like to see assembler C Cobal Pascal and Fortran added to the next few additions of visual studio i still have shrinkwrapped factory sealed copies of ms assembly for dos and same for visual studio upto v5 vb learning edition... while I like C#'s evolution into XNA GS add-on to visualstudio it just shows how durable and flexible the visual studio compiler and IDE that its becoming from its roots into version 11 granted I adore metrowerks code warrior for my playstation 2 and Net Yaroze programming needs not to flame or start a fire customer service and support sucks removing my liscense from their activation service because version3 has been discontinued now leads me to break out my hex editior and do some old school cracking... wich as a dev makes me sick to my stoumache and soul to stoop that low to get what I paid ov $18.5k for for a few years back

        Xeveniah Darkwind

      • Michael Paterson commented  ·   ·  Flag as inappropriate

        I definitely disagree with this one. I love to organize my classes with regions (Fields, Properties, Ctors, Methods). Maybe just add a setting to "hide" regions or something.

      • Ryan Peters commented  ·   ·  Flag as inappropriate

        I agree. My dog Fluffy also says that Intellisense is akin to communist Russia and should be removed. You should REALLY hear what my flying unicorn has to say about auto-formatting!

      • Floele commented  ·   ·  Flag as inappropriate

        I always enclose properties in #region to be able to collapse and expand them all at once. I don't see what is evil about that and I would not want this feature to be gone (even though it can certainly be used to make hiding ugly code easier, but removing regions is not a method to stop people from writing bad code).

      • Bobby Dranzek,KG4QGK commented  ·   ·  Flag as inappropriate

        yup huge waste of time and resources i miss my old copy of visual studio 6 enterprise with my copy of back office small business server4.5

      • Anonymous commented  ·   ·  Flag as inappropriate

        Agreed, waste of time. The only use for them is to get the Regional Manager achievement.

      • Tudor Turcu commented  ·   ·  Flag as inappropriate

        uservoice.com shoudn't be used for that (personal opinions and tastes).

        Yes, in an ideal world regions would not be needed, everybody would write clean code like "Uncle" Martin and all that stuff - but in the real world very, very few people have heard of SRP, clean code etc, and usually classes have many methods, properties, fields, events, especially on UI-side, where regions are very usefull in organizing the code in a less than ideal world.

      • Robert Sewell commented  ·   ·  Flag as inappropriate

        Too bad you don't explain in what way #regions are used for "evil", or how you came up with that statistic. That would give people better points of agreement, or something more substantial to defend their positions on the issue.

        If you don't like them, don't use them. If you work with people who use them in ways you don't like, educate them.

        Just because you think they're evil, or you work with people who use them in ways you don't like, that does not justify removing a feature that can and is used in ways that are beneficial to others. Maybe I should suggest MS remove the "I suggest you ..." feature of this web site because my informal analysis concludes that K. Scott Allen uses it for evil 96% of the time. :)

      • John commented  ·   ·  Flag as inappropriate

        I disagree that they're evil. If you don't like them, don't use them. If you don't want them on your team, make an institutional guildline that says your developers shouldn't use them. If you don't like to use open source projects that use them, again, don't use those projects.

        As with anything, the #region can be abused making things hard to read / follow. In moderation though, it can make a structured code file much easier to read (for instance, if you grouped your categories in a few simple groups, constructors, methods, properties, etc.).

        To each their own. "They're evil" is a little meladramatic.

      Feedback and Knowledge Base