Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
ASP Insider

Microsoft Store

Using Regions in Code Files

Recently I have been confronted with a movement that seems to hate using #Regions in source code files. Personally I really like using regions and I absolutely like the ability to collapse sections of code I am not directly working with. The opposing argument I am given is that my classes or methods must then be too long. I really have to disagree, I work hard at making my classes, methods and properties as refactored as I can get them. But I do like to organize my code into related sections. And sometimes there is just a lot of logic that goes into creating the UI or business logic and regions help keep things clean.

I wanted to show an example from a custom Exception class I was creating today. It consist of two custom properties, PropertyName and PropertyValue. I wrapped these properties into a region and moved them out of the way. Once I have the properties created there should be nothing else I need to do with them and by wrapping them in a region I can hide them out of view.

This reduces the amount of code that I need to slide through to get to code I am working with at the moment. It also forces me to organize my code into common areas or ‘regions’. This alone is worth it to me because it gives me a nice way to toss my code into a bin of sorts to keep it organized.

The next region I created is to hold my constructors. Because this is a custom exception class I created three new constructor overloads. This gives me the ability to set my custom properties and call the base constructors for the Exception class. Again I have all my constructor overloads contained within one area in my file and I can expand and collapse it as needed.

What are my alternative choices? Creating several classes that either inherit from a base class or create numerous partial classes with partial methods. Each of these hold their own merits but to me serve different purposes from what regions are designed to do in Visual Studio.

I invite those with opposing views or that can add to the case for using regions to please leave comments on the topic. This is a small class where I show how I really like to use #Regions. There is not an overwhelming amount of code in this class, but the #Regions help keep things organized.

A special note, I have CodeRush and Refactor Pro from DevExpress installed on my machine. This makes the display of the regions look different than the out of the box look. The functionality is still the same, but I just wanted to point that out if you were feeling a little lost.

 

 

 

 

 

Share this post :

Posted: Thursday, July 31, 2008 9:13 AM

by Chris Love

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# July 31, 2008 9:16 AM

Derek Lakin said:

I'm with you all the way. I love using regions to tidy up the code and hide away things I don't need to look at right now. I also use Omer Rauchwerger's Regionerate tool (http://www.rauchy.net/regionerate) to help do the tidying up for me :)
# July 31, 2008 11:26 AM

Nuno Gomes said:

Hi Chris, I totally agree you, #regions got nothing to do with code complexity, its all about keep things organize. Often my colleagues tell me that I create too many regions but I simply can't escape from creating a Properties region even if there's only one. Another thing I notice is that usually people only put the region description in the #region and dont update the #endregion. I try to always put description in both sides since I found it very useful when multiple regions are expanded.
# July 31, 2008 11:35 AM

Jason Bock said:

I'm not a big fan of #regions, simply because they've been an easy way for devs to hide lots and lots of code. You may be disciplined enough to keep the classes small, but most examples I've seen use #regions to smuggle hundreds and hundreds of lines of code in. Personally, I like seeing the code right away when I open a file. #regions are just noise to me. It's a personal preference but if I get to write the coding standard, #regions are pretty much verboten :) Regards, Jason
# July 31, 2008 12:02 PM

David L. Penton said:

I love regions and would hate it if there weren't there :)
# July 31, 2008 12:39 PM

Schneider said:

I agree, I wish I could collapse If blocks also, seems I end-up working on a lot of bad code...
# July 31, 2008 1:27 PM

Karthik said:

I have a couple of problems with regions: 1) They often encourage extremely large classes thereby negating SRP. 2) They often hide large amounts of code duplication thereby negating DRY. Most people who hate #regions feel that way because of seeing some fairly nasty code bases where SRP and DRY were not followed as a developer discipline.
# July 31, 2008 2:12 PM

Chris Love said:

Karthik, what is SRP and DRY?

# July 31, 2008 3:05 PM

Justin Etheredge said:

SRP is Single Responsibility Principle, and DRY is Don't Repeat Yourself. These are principles that every developer should be following to an almost religious tenacity. I agree with you that regions are good things when used properly, but they are often used to do improper things. I am not the kind of person though that believes that you shouldn't give developers sharp tools because they may cut themselves. The problem isn't the regions, it is the programmers. One thing that I do hate though is using regions within methods. In my mind, there is *never* a good reason to use a region within a method.
# July 31, 2008 3:12 PM

Dana Coffey said:

Regions are great, but like many features of .NET and Visual Studio, they can be enough rope to hang an APP if used improperly. I definitely like them to tuck away properties or bits of code that will not need to be revisited. And yes, I've burned myself by not having the visibility of seeing ALL the code when I've tucked something away into a region. So in a rambling way, I'm saying yeah regions rock, but less is more :)
# July 31, 2008 3:30 PM

Arcane Code said:

Regions are like anything else, when used properly they can make working in a class file easier. You can find code more quickly and just focus on the areas you are working in and hide everything else. And like other coding tools they can be abused as well, to hide vast amounts of bad code. That's why software development is a discipline as much as it is an art form. Personally I like regions, they make it much easier to organize and view my code at a high level.
# July 31, 2008 6:09 PM

Chris Love said:

Thanks Justin and Twitter peeps on the SRP and DRY TLAs. They mean write code the way I write code, but here are the TLAs you were never aware of that describe your practice. ;>

# July 31, 2008 8:02 PM

Morten Lyhr said:

I wrote about posible abuse of #regions, and why they are a code smell here: http://morten.lyhr.dk/2008/07/visual-studio-regions-are-code-smell.html
# August 1, 2008 5:11 AM

Kevin said:

Regions == Evil.
# August 1, 2008 9:31 PM

Mike Borozdin said:

Personally, I love using regions because they they enable me to toggle the blocks of code I don't need to see.
# August 2, 2008 9:18 AM

Bart Czernicki said:

To me the debate of #regions possibly hiding long code is ridiculous! Are we to believe that the most beautiful & well factored code would be written, but a developer decided not to because they could be lazy and smuggle with #regions? Chances are you have poor developer. What is a definition of a small class? Take a look at some of Microsoft's code in reflector and tell me that they keep the size of the class code small, organized and readable. I know everyone is all about "Spartan programming" and contests with doing x in 100 lines of code or less. Its all nice. Ur not going to convince me ur code is more elegant because its nice and compact and region free. What will convince me is how this compact code helped u deliver the app faster/less errors/more maintainable and made u more $.
# August 2, 2008 8:08 PM

Visual Studio Hacks said:

My latest in a series of the weekly, or more often, summary of interesting links I come across related to Visual Studio. Jonathan Wood has created a new video - How Do I: Use the Visual C++ 2008 Feature Pack? Part 2: Using the Ribbon Bar . Greg Duncan

# August 3, 2008 10:40 AM

Andrew said:

I'm not sure about putting events and methods into separate regions. When you are looking for some code, you don't necessarily know how it was implemented. If regions are based on functionality, you have an indication of where to look. For instance: put all the user events, user methods and user functions into a region called "Users".
# August 3, 2008 10:54 PM

LazyCodeSlinger said:

Chris, I absolutely agree with you. It was nice to see someone take this stance after what Jeff Atwood had to say about the same subject just a couple of weeks ago. Don't try to convince me that regions are bad. It's nothing more than a way to organize your code. You wouldn't put public properties in the middle of an area where your methods are, would you? It's simply a higher-level of organization.
# August 4, 2008 8:10 AM

charris said:

/////////////////////////////////////////////////// // Constructors Yeah, I appreciate the region feature as well. :)
# August 4, 2008 9:25 AM

Chris Love said:

Andrew, That is a judgement call and I try to organize concerns into a region and then the different member types in sub regions. Rarely do I have large classes that really need that depth though.

# August 4, 2008 5:29 PM

Mike Doyon said:

I agree on the use of regions. I also tend to use regions in a very similar manner as yourself. One difference I have found helpful, however, is also using partial classes to split the declarations portion of a class into a separate file. This does require discretion as it only applies to classes with enough properties and/or global declarations to make it worth it. Basically, I mimic how Visual Studio splits a winform project into 2 source files. Typically, I would split the properties and declarations into a partial class file if they could not all be viewed on the screen at once. For the implementation of the code, I tend to group by either functionality (usually this is a sign the class can be refactored) or by accessibility.
# August 5, 2008 8:53 AM

Clay said:

I like the ability to collapse HTML and XML...and have never seen an uprising around it. I view Regions in the same way. Anything can be bastardized. I suppose we should expect to see articles from Region-haters griping about the misuse of tabs, line breaks, and perhaps font choices. To complain about Regions makes me think some folks may need another hobby.
# August 6, 2008 9:34 PM

DevTopics said:

Code outlining (regions) can collapse a 50-page code file into a single screen and save hundreds of clicks and endless scrolling. Code outlining is so important to disabled developers that I listed it as one of the top 10 advances in software development: http://www.devtopics.com/top-10-advances-in-software-development/ I can't imagine going back to a giant flat code file. It would be like working with Windows Explorer and no folder hierarchy. Imagine if every time you opened Explorer, it showed every file on your hard drive in a single window with no organization. I haven't seen the same backlash against regions you're talking about, but it's most likely from developers who are producing only trivial programs.
# August 7, 2008 9:25 AM

deesstar said:

hmmm, regions. i used to use them all the time but in the past couple of years i have stopped. i realized that i when i opened a file the first thing i tended to do was expand all regions. eventually it dawned on me that i could save the key strokes by just not putting the regions in in the first place :s. so shoot me. i do tend to spend time logically ordering all my methods and properties so it is easier to read as i scroll down the page.
# August 19, 2008 8:54 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS