VS.NET and Code Regions

I'm currently working on a project where almost every function has its own region. At first I found this convention onerous, but as I used it, I saw why it was necessary. The default Visual Studio .NET outlining support leaves a lot to be desired. Take your typical commented Page_Load method:


This is a companion discussion topic for the original blog entry at: http://www.codinghorror.com/blog/2005/07/vsnet-and-code-regions.html

My personal pet peeve is that C# lets you have a region within a function, but VB doesn’t. In general, my functions are short and modular, but every once in a while I want an intra-function region in VB and you can’t do this.

I think your scheme is compatible with the “every function has its own region” scheme, too. There would just be a little bit more hierarchy there.
Note that I didn’t come up with this scheme, it’s just what is being done… when in Rome, etc.

This has always served me well.

I don’t disagree, but why can’t the editor automatically (or optionally) show things grouped this way? Why do we have to set up a bunch of custom regions to do its job?

The next best thing is a macro that sets all this up. Sitting there typing in a bunch of mechanical, identical #region defs is a waste of our time…

I use a similar style to Marty and it works quite well.

I have to admit, I love the outlining feature that was introduced with VS2003.NET, but I feel that developers miss use it.

Adding a region to each function/method is just overkill, and if were reading your code I would spit it back at you in annoyance. What ever happened to writing nice neat methods, and keeping the number of methods in a class to short number? If you really need to outline all your methods because the code in your classes is overwhelming then you should consider abstraction with base classes and less complicated methods.

I usually only separate the fields, constructors and static members from the methods using regions (one for each group), which is enough so that my code be readable.

I like being able to “see” all the code at once, and hiding some sections of the code makes me uncomfortable.

That’s precisely why I think the “folding” ought to be automatically handled in the editor, so you don’t have to litter your code with a zillion #region #endregion statements.

Adding a region to each function/method is just overkill, and if were reading your code I would spit it back at you in annoyance.

This isn’t my personal convention, it is a convention of a project I am working on. Personally, I don’t use regions all that much. That said, I can see why they’re going to these lengths, because the default editor “folding” is not very good…

Whenever I have a new VS.NET setup, the first thing I do is to set outlining off. Yeah I know a lot of developers love this feature. Call me old school, but somehow it doesn’t work for me. I like being able to “see” all the code at once, and hiding some sections of the code makes me uncomfortable. I’m still mostly a PageUp, PageDown, search for text-type coder.

I too happen to think code regions are misused by many. But really what it comes down to is what do you want from the regions, how do you want your code organised. Much of it has to do with how you format your code.

At the moment I’m using ‘Event Handlers’, ‘Properties’ and ‘Methods’ as regions. Constructors and variable declarations go at the top. I’m not a fan of ‘Private This’ and ‘Protected That’, I want my code grouped by purpose. But really it comes down to how you format your code.

I’ve seen developers go through stages with regions. First, they ignore them, just seeing the VS-inserted blocks (web forms init and events etc). Second they realise they can use them. Third, they overuse them.

Then one day they are searching for some code, and for some reason, the ‘search hidden text’ checkbox is not checked, and the code in the collapsed regions does not get searched. They miss something important, because they assume they are searching the open file by by default, not just the uncollapsed regions of the file.

In VB, you at least have the option of using Procedure separators. These combined with the Class Viewer do a nice job of navigating code without too much extraneous effort.

Of course, I’m using VisualAssist which has a nice surround in region function… :slight_smile:

I’ve worked with some developers who use regions to their detriment. They build methods that are multiple screens long and then rely on regions as an organization structure. The drawbacks?

  • regions are just fancy comments. So it’s still a really big functional block.
  • regions don’t scope variables. The really big functional block is sharing variables through the other regions.
  • big functional blocks encourage spaghetti code. Since regions give some developers comfort that their code is clean (and it does appear to), it allows them to share those variables all through the functional block, which like spaghetti, makes it hard to later refactor into smaller functional units.

Until I started working with such individuals, I saw regions as a curio. But now I’m not so happy because the abuse of them leads to more work later on.

Stop code abuse!

#region “Comments”
/* Modified By : Kiran K
Modified Date: 29th October 2007
Modified Reason: For Globalization
Description:

I think regioning off every function is a bit overkill. I generally organize my code into:

Instance Members
Instance Properties
Instance Methods
Constructors
Static Class Methods
Static Class Members
Events

This has always served me well. If I end up having to write a horribly long method, I will probably region it off. I don’t do that often though, as I have a short attention span and I forget what I’m doing when the method declaration scrolls out of view. :wink:

Hi Jeff - Please consider opening a feature request up at the MSDN Product Feedback Center so that you can tell us exactly how you’d like to see this improved.

http://lab.msdn.microsoft.com/productfeedback

Cheers,
Aaron

“Then one day they are searching for some code, and for some reason, the ‘search hidden text’ checkbox is not checked”

What really sucks is that by default that check box is not checked. It really should be checked in a fresh install. I didn’t realize for the longest time that collapsed regions weren’t searched unless that was checked.