Region is an excuse for hiding large files

Often when I take a look at sample code there are a lot of regions. I need to open them all the time, it really start to make me crazy. Why does people even use Region in their code, what's the point of hiding code? I think it's only an excuse to hide code because of a too large file. What do you think?

24 Comments

  • Personally I don't really like em... Very annoying to expand code... When reading code I want to see the code in front of me not doing lots of other insane things...

    I think most ppl that use regions also use them to think they got cleaner code in there already bad code. Hide things make code look better and smaller.

    I think too many people aren’t aware of refactoring or know how to use it and use region as a excuse instead.

    It's the same argument why not comment code. It's not needed if you can code.

    best Johan

  • One has to get into the habit of having just the right amount of code in a file. Say about 150 lines inclusive of blank line fillers.

  • Just use the 'Ctrl+M Ctrl+L' keyboard shortcut to expand ALL outlining.

  • Agreed. Not too long ago, I thought they were the greatest thing ever. I organized everything into neat little regions, like sorting silverware.

    These days I see them hiding messes, like magic. Poof -- all that ugly code goes "away". Unfortunately it's only sweeping it under the rug. My reaction now is to remove them on sight so I can see the mess and can do the right thing via Extract Class.

  • I still use them out of habit, but they are starting to annoy me more and more. I think it's time to break the habit; my time is better spent writing code, not disruptive comments.

  • Named regions as any other tool have to be used carefully. But people often abuse it. But sometimes it is helpful, e.g. hiding the region with disclaimer text ...

  • I used to love them but I had to kill them.

  • Regions can definitely be abused. I used to use them a lot to group properties and constructors into regions. But right now I almost never use them.

  • Agreed - I HATE regions. Know how to change the Visual Studio settings so that it doesn't add Interface implementations into it's own region when hitting tab to implement it?

  • I just remove them whenever I find them. If you happen to accidentally leave unchecked the "search hidden text" option in the search dialog, then the code is effectively invisible! Very annoying...

  • I think I know about one legitimate use of large files and regions:

    When I write tests, I make one test class per one tested class, e.g. FooTest test fixture to test Foo class. I usually write many tests to test one method. So if the Foo class has 5 methods, FooTest can have 25 methods. And I think it's not such a bad idea to use regions to group the tests that test one method.

    What do you think?

  • Just say no to regions.

  • Personally I love them. I like the way I can structure code using them. Ctrl M + L is your friend when dealing with them. Also I find my classes are not too big with regions, but maintaining a piece of code can be a pleasure if you've split it up properly. Also Sometimes I prefer having more regions than files.

  • I could not agree more. I have a coworker who is obsessed with them and it drives me nuts! Regions should not be substituted for comments.

  • If you will add description along with region start, you will find them useful because you know what that region is doing, without even expend that region.
    e.g.
    #region Write log in log file
    #endregion Write log in log file

  • Dileep,

    I wont say you should never use regions, because I believe they could be useful sometimes. However, in your example, the region could easily be replaced with comments, or -even better- a method call. i.e. Instead of doing this:

    void SomeMethod()
    {
    // do some work

    #region Write log in log file
    // some log work here
    #endregion Write log in log file

    // more work
    }

    You could do this:
    void SomeMethod()
    {
    // do some work

    WriteLogInLogFile();

    // more work
    }

    void WriteLogInLogFile()
    {
    // some log work here
    }

  • You know it is possible to turn them off :)

  • Saying that regions propagates bad coding habits and that people that do use them are unaware of refactoring is going a bit too far if you ask me :) What's a large file? 150 lines? 500? 1000? It's in the eye of the beholder (even with refactoring applied)

    Regions serves a purpose, if you want them to. They're optional and you can choose to use or not to use them - that's you prerogative. Like all programing aids (yes it's an aid) it's only as good as you use them. Learn how to use them in your environement (shortcuts for example) and find a style which suits you / your team and there's nothing wrong with them.

    I personally hate declaring all of my private/protected fields at the beginning of a file (despite what StyleCop says) and find it more suitable to group them with the property (the times I cannot use implicit property decelarations).

    Yes you can abuse regions, but the same can be said for just about anything in the context of software engineering. Use with moderation and best of all, choose to use the tools which best applies to your style and situation

  • Regions within methods I agree - it's just hiding. But I like using regions to structure file and I believe it works well.

    Class regions for member variables, properties, public methods, private methods etc. Makes it easy for everyone to know how to structure the file and makes it nice and clean.

  • I like regions. I find them useful for grouping things like 'Public Properties', 'Events', 'Private Methods', etc.

    I never used it to hide a large file though. I think regions should not be used to group classes inside a single file (every class should live inside its own file). I always have my line numbering turned on so I know exactly how big a region or a file is - I dont think you can really hide anything using a region. With Ctrl-M Ctrl-L and Ctrl-M Ctrl-P shortcuts you can easily discover what's inside each region.

    Imagine how a windows forms class will look if you dont have regions and partial class. Your class will be bloated with all designer generated code and it'll be pretty hard to scroll to actual event handling code. My 2c. :)

  • Well, to be honest, I'll kill anyone that asks me to read their code and then I find out that it has lots of regions on it. Is this too extreme? :)

  • I often use regions (or at least comments) to separate different aspects of my code - i.e. Databinding, initialization, etc... It just makes it easier to scan for sections. But never, ever, within methods - I didn't even know you could do that :-).

    In addition - pre partial classes, it certainly did help to keep all the designer stuff out of the way.

    Hal

  • In some training situations where we teach C# beginners classes we also found the region construct to do a lot more damage than good. As a new Visual Studio user, you are often on the edge of what the human brain can handle with the enormous feature set. Trying to read some code cluttered up by regions can then cause serious accidents...

  • Region is a code smell.

    If you need regions, it's sure a indication that your class is doing to much!

Comments have been disabled for this content.