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?

Published Thursday, September 18, 2008 7:23 PM by Fredrik N
Filed under: , ,

Comments

# re: Region is an excuse for making large files

Thursday, September 18, 2008 1:30 PM by Johan Normén

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

# re: Region is an excuse for making large files

Thursday, September 18, 2008 1:47 PM by Gunshu

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.

# re: Region is an excuse for making large files

Thursday, September 18, 2008 1:50 PM by Artem

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

# re: Region is an excuse for making large files

Thursday, September 18, 2008 1:58 PM by Sean

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.

# re: Region is an excuse for making large files

Thursday, September 18, 2008 2:08 PM by Jon Sagara

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.

# re: Region is an excuse for making large files

Thursday, September 18, 2008 2:16 PM by Aliaksei

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

# re: Region is an excuse for making large files

Thursday, September 18, 2008 2:43 PM by Morten Lyhr

I wrote about the same topic here:

morten.lyhr.dk/.../visual-studio-regions-are-code-smell.html

# re: Region is an excuse for making large files

Thursday, September 18, 2008 2:50 PM by Battaile Fauber

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

# re: Region is an excuse for making large files

Thursday, September 18, 2008 5:23 PM by Torkel

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.

# re: Region is an excuse for making large files

Thursday, September 18, 2008 5:58 PM by Jonas Follesø

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?

# re: Region is an excuse for making large files

Thursday, September 18, 2008 7:14 PM by Mauricio

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

# funny wallpaper » Region is an excuse for hiding large files

Pingback from  funny wallpaper » Region is an excuse for hiding large files

# re: Region is an excuse for hiding large files

Friday, September 19, 2008 2:07 AM by dkl

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?

# re: Region is an excuse for hiding large files

Friday, September 19, 2008 4:47 AM by Joe Chung

Just say no to regions.

# re: Region is an excuse for hiding large files

Friday, September 19, 2008 5:38 AM by Mladen Mihajlovic

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.

# re: Region is an excuse for hiding large files

Friday, September 19, 2008 8:28 AM by mhildreth

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.

# re: Region is an excuse for hiding large files

Friday, September 19, 2008 8:55 AM by Steven

I agree. We used to have a coding guideline that forbids the use of regions within methods. The new guidelines simply state that we don't use regions.

Using regions will easily lead to big files. I recently fell in this trap. In the unit test project of my argument validation library (www.codeproject.com/conditions) I used regions heavily to group all the tests per library method. It felt okay at first, but some files are now so big, that Visual Studio becomes to slow. And it got even worse after I installed ReSharper. The situation now is unworkable. I’ll have to refactor it, but I only have to think of a better structure for that code.

# re: Region is an excuse for hiding large files

Friday, September 19, 2008 9:09 AM by Dileep

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

# re: Region is an excuse for hiding large files

Saturday, September 20, 2008 8:08 AM by svdeursen

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

}

# re: Region is an excuse for hiding large files

Monday, September 22, 2008 4:47 AM by Andreas

You know it is possible to turn them off :)

# re: Region is an excuse for hiding large files

Monday, September 22, 2008 9:38 AM by Andreas Håkansson

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

# re: Region is an excuse for hiding large files

Tuesday, September 23, 2008 2:25 AM by Martin S

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.

# re: Region is an excuse for hiding large files

Tuesday, September 23, 2008 9:24 PM by Josh Thuya

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

# re: Region is an excuse for hiding large files

Wednesday, September 24, 2008 4:57 PM by Luis Abreu

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? :)

# re: Region is an excuse for hiding large files

Thursday, September 25, 2008 8:06 PM by widescreen

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

# re: Region is an excuse for hiding large files

Thursday, October 02, 2008 7:35 AM by persa

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

# re: Region is an excuse for hiding large files

Saturday, October 18, 2008 4:08 PM by Benny Thomas

Region is a code smell.

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

Leave a Comment

(required) 
(required) 
(optional)
(required)