The Problem With Code Folding


What’s so great about not organizing your code? I mean, I want my programming job to be easier, faster and keep me as much as possible from making stupid mistakes.

Also, there are reasons for being nice to the editor. In fact, I think being editor-friendly is a FEATURE of a language. For example, in C++ it’s often hard to figure out where an actual ERROR is, because of an unclosed quote and the semicolons might be all messed up for a long time down. But if line breaks ALSO end a statement, your development time will be quicker over time.


Holy cow. Talk about short sighted.

You don’t organize code? Really? You don’t place functions and procedures together that all relate in your source files?

Folding has a place and I’m very surprised that you don’t see that.

There are a lot of times that we have HAD to use that #Region just so that we can easily find things. You try searching a large file for a word that gets repeated 50 times throughout the whole class file… As soon as we started using #Regions properly, life was MUCH easier and WAY more efficient.


I’ll agree with the other folks here, regions are awsome. Sure, they can be overused (regions within regions within regions). Normally when I open a class, I want to go to a specific part of it. It is rare that I want to see properties and private fields, but when I do typically I don’t want to see the methods. And it is only once in a very long time I want to see my references.

Regions make it so that is handled pretty elegantly.


I think Jeff is wrong on this one. #region is very handy.

In addition, the IDE will fold code on any block level statement, not just regions.


Here is the problem with regions.

When I open up a file in VS, I usually want to see collapsed code, like this:

public string AddressLine1[...]

public string AddressLine2[...]


I have keyboard shortcuts for the various outlining commands: Alt-Right to expand/collapse all, Alt-Left to CollapseToDefinitions, and Alt-Down to toggle outlining open/closed for the block I’m in.

But regions completely screw up outlining. If I expand everything, I get this:

#region Properties
public string AddressLine1
    get { return addressLine1; }
    set { addressLine1 = value; }

public string AddressLine2
    get { return addressLine2; }
    set { addressLine2 = value; }


Too verbose. Whereas if I collapse to definitions, I get this:


Now I can’t see anything. So to get a collapsed view of all the code, I have to collapse everything, then individually open each region, which is an annoying waste of time.

It’s easy enough to group things in terms of methods, event handlers, properties, etc. with comments. We don’t need to use regions to do it.

Regions should only be used for boilerplate code that doesn’t ordinarily need to be seen. Designer code is one example of this. C# event declarations (as someone referred to above) is potentially another. Any code that regularly needs to be edited, or that one needs to be aware of when adding to a class, should not be in regions.


This is too funny — I often find myself reading Jeff’s stuff and trying to decide if he’s right and I’m just old and crotchety about the way code should be (80 char width, hungarian notation, intellisense is the crutch of the weak, etc.) … today I got to see Jeff turn the corner into old and crotchety too.

Hey you kids, get out of my yard!



If you don’t like using #region then don’t use it. It’s purely a personal preference thing. What I hate is empty regions. You end up clicking on all these regions trying to find the method you need.


‘Folding can hide deficiencies in your editor. The presence of so-called standard boilerplate regions like Public Constructors and Public Properties and Events is not a feature. It’s a bug. The editor should automatically offer to fold up these common structural blocks for you! I’m continually amazed that programmers spend time doing this scutwork when they could be writing useful code. Or at least demanding a smarter code editor.’

Folding can hide deficiencies in your editor, while your editor can hide deficiencies in your language. I am frequently annoyed when C# (or C++, or Java) requires lots of ugly boilerplate code. Any time I find myself writing the same pattern of code several times I start looking for ways to avoid the duplication, but often I get to a point where it comes down to fundamental limits of the language. Perhaps I would be happier writing Lisp.



It was bad enough having to strum my mouse wheel 20 times to get to this input box… (please do something about that :slight_smile: )

I fixed this problem for you. Next time you want to jump right to the input box, just press the ‘End’ key. You might have to clear your browser cache for this fix to get downloaded to your machine.


There are a lot of times that we have HAD to use that #Region just
so that we can easily find things. You try searching a large file

I don’t have your code in front of me so I can’t really make any judgements. However, I’ve found over the years that if a file gets really large, tough to navigate around, and/or tough to read, it almost always needs some funtionality split out of it and put into another file/class.

My suggestion for users of this #Region thingy would be to instead physically take that code you want to hide out and put it somewhere else.


I think it’s overuse that drives people mad not specific use. I often write stuff that uses crystal reports so can have 10 to 15 buttons on screen usually in a tab page each to run a separate report, they all have pretty standard code behind (not the same I couldn’t wrap it into a call to a function or anything) and I hardly ever need to see it so I prefer to put it in a crystal reports button handler region. That means my core code is left visible the bit I know is correct is left alone. I don’t think many people would object to that.

Over/Misuse is constructors, methods, helper methods, properties, fields… I often think people who do the regions this way are maybe missing the objective of the region it’s not to separate your properties from your methods most of the time you need them both on screen.


Scanning code took too long for me so now i use the class selector and member selector drop downs, and then use regions to keep the code clean (of course, i don’t make the regions myself, i have use regionerate to group and sort everything).

But that’s just my preference


Good comments Jeff. I’ve long since strayed off the MS path and its amazing how much a free product like Eclipse can still trump the golden child of IDEs (VS) after all this time. Granted, IBM has dumped buttloads of cash into Eclipse but still…no excuse for mucking up your LANGUAGE with something that a smart editor can handle. Rofl, cakes.

I think crap like this is whats pushing talented coders away from the lower level languages and making them fall in love with Ruby and Python. They just want to get stuff done and not worry about junk that slows them down.


You’re spot on! Regions seem to flap about and get in the way of getting on with it. And dangerously folks seem happy to dunk stuff into a region, when they’re too lazy to refactor code into a new class.

And Ctrl-I … so few of my colleagues seem to use it. It makes me cringe when I see people Ctrl-F’ing slowly about because they don’t bother to learn their tools. And, why do so many programmers seem to refuse to learn to touch type? It only took me a couple of weeks to get into, but makes working with a computer soooo much more fluid!


You have good points there. I personally don’t want to see loads of verbose code that is the result of adding some fancy new technology to the project. But that verbosity is possible to be hidden by encapsulating, not with regions.

For example you all remember The Evolution of Programmer example of coding levels: Basic programmer, advanced programmer, expert programmer etc., where the basic programmer does only 10 Print Hello World 20 Run, but master programmer has already pages of advanced C-code.


This is obviously a preference that can be overused. Just like pretty much everything else, it has its place if used correctly.

I feel more strongly about overuse of comments and even worse, inline suppression of Code Analysis…talk about making your code not readable.


Rock Scroll. Use rock scroll. See your code from on high and from down low all at once. Avoid regions. Use rock scroll. Rock scroll ftw.

Damn it’s good.


Hmm. I was going to argue that with proper use, #region is not so bad, but then I remembered a recent gem from a college. Here’s a rough outline:

if (some condition)


I use Vim, by the way, and it’s configured not to fold anything unless I explicitly ask it to.

Right, Vim does it correctly, I think, and has a tremendous amount of power available to it (though you have to slog through a bunch of documents to see all the features).

It folds on a number of things, none of which masquerade as preprocessor statements.

Have a tremendously long switch statement containing a line or two of code for every letter in the alphabet? Tap two keys and vim folds down all the code within the nested brackets, allowing you to just look at the case statements. Etc.


Sorry for the double post… keyboard error.

Hmm. I was going to argue that with proper use, #region is not so bad, but then I remembered a recent gem from a college. Here’s a rough outline:

if (some condition)
#region Foo


if (some other unrelated condition in another block)