a companion discussion area for blog.codinghorror.com

The Problem With Code Folding



Or in a different class.

Totally agree, regions cause friction.

Resharper can help with that btw.


Used correctly, code folding HELPS you see code. In this scenario, it hides those three ugly pages of logging code so that you can see the code that matters. If you want to see everything, it’s an easy Ctrl+M, Ctrl+L.



100% agree. #regions blow.

Might as well have called it



It is possible to configure the Visual Studio IDE to not fold any of the regions when files are opened, but this is the out of box behavior, so that’s what most developers will see.

Well there you have it then. You set up your VS like that and you start working. There, wasn’t that easy?


The worst thing about #region to me has nothing to do with whether or not code-folding is a Good Thing or a Bad Thing. It’s that it breaks a mental convention that I’m used to seeing in curly-brace languages, which is that lines beginning with a hash invoke some sort of compiler magic. (Think #include, #if, and others from the bad old days of preprocessors).

The first time I looked at C# code with #region in it, I didn’t know what to make of it, it actually disrupted my original task while I went off on a 20 minute tangent of reading docs to assure myself that it didn’t in fact invoke some sort of compiler magic. I use Vim, by the way, and it’s configured not to fold anything unless I explicitly ask it to.

This is rediculous to me. As far as I can tell, #region is completely ignored by C# compilers, yet there is already a syntax for making the compiler ignore things: it’s called a comment. I can’t find anyone who can give a compelling argument why a different syntax is needed for this particular compiler-ignored directive.

We regularly write comments designed to be parsed by IDEs, documentation tools, and even static analysis tools (vis SPARK Ada’s formal verification mini-language). Why is code folding special in the VS universe? Numerous full-featured editors such as Vim have no trouble folding code on commented directives (or on other tokens of the language for that matter), and the commented syntax makes it instantly obvious to people using editors without direct folding support that the directive is semantically inert with respect to how the code will compile and run.

This discrepancy put a bad taste in my mouth for #region automatically, regardless of it’s other merits. On the infrequent occasions I put fold markers in my C# code, I usually end up writing // {{{ Vim-style markers just so I don’t have to be mentally jarred by seeing something that years of curly-brace languages have trained me to think of as a compiler directive, but which is actually isn’t.


Heh. I haven’t used .NET stuff before. I have to say that I don’t find it a good sign when a laguage has a Jedi-mind trick primitive.

These are not the lines of code you are looking for. (waves hand) They can go about their business. (waves hand) Move along.


It boggles my mind that we’d add significant lines of code to our project that do nothing but offer organizational hints to the editor.

Spoken like a true zealot. If the amount is significant, than you must have 10 lines of code overall.


The editor should automatically offer to fold up these common structural blocks for you!

Whaa? So folding IS good?

I have found 3 problems in your argument, and I must come to the conclusion that you are writing this for the views. Well, you have to make a living so do what you feel you need to do, but you are out of my rss reader sir.


Having to actually put something in your code to support folding sounds brain-dead to me. I use TextMate, and it handles folding automatically, at various levels. I use it mostly with specs, since I often want to see the specs rather than the code that verifies them.
describe MyClass do
it should do something do

it should do something else do

describe when doing something do
it should keep track of foos do

describe when not doing something do

And it is nice that it remembers exactly how I had the code folded when I last closed the file, without me having to insert a bunch of stuff into my source code.


Code folding is a useful feature. 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: ) doing it in a code editor when I’m trying to remember complicated constructs is worse. Take me, quickly, to the source I want, and then don’t confuse me with lots of clutter I don’t want to see.

Boilerplate code is stuff we never edit, so we don’t need to look at it.

There’s a lot of things Visual Studio could do better if Microsoft bothered to copy the competition…

And there’s a lot of things they’ve done very very Right.

Go use Eclipse, XCode or KDEvelop :wink:


Automatic folding? Use VIM. :slight_smile:


I fail to see the problem. You say that you can configure your IDE to expand all by default, so why not just setting this one option and live happy? If other programmers prefer to have these collapsed, why forcing your way upon them if there’s an easy way to workaround it?


First of all a lot of people are getting the impression that the only way the VS IDE will do code folding is with regions. This is not the case and the post is inappropriately titled because Jeff seems to only have a problem with the region directive.

Frankly its an opinion not fact. If you don’t want to use regions you don’t have to, if I want to use regions I will whether or not Jeff Atwood likes them.


If learning to live with #regions in Visual Studio, I recommend changing the background colour for preprocessor directives. This makes #region markers stand out well as you scroll past them.

I also strongly recommend getting a mouse with a flywheel-style scroll wheel. I got a Logitech VX Nano for my laptop and now I use it with my desktop whenever I can. It makes it much easier to scroll through long source files or other documents. With a flick you can send the wheel spinning to scroll quickly through even very long files, while you still have fine control when you need it.


#region has zero meaning to the compiler; it’s a hint to the editor to allow code folding. It doesn’t do any namespacing or scoping. Why, exactly, are we writing code to accommodate the editor?

I strongly agree. And in the same way: type declarations add zero meaning to the program logic; it’s a hint to the compiler. Why exactly are we writing code to accomodate the compiler that adds nothing to the program (except to prevent you doing all sorts of stuff…).


The suggestion to simplify one’s code by splitting it into to several partial classes is laughable. Partial classes came about purely as a result of the way Visual Studio’s form designer works. Otherwise they’re a terrible idea IMHO.

PS. Visual Studio doesn’t require having to actually put something in your code to support folding.


Where do you see the difference between folding and using different files for different classes ? Both hides lots of codes behind short identifiers, so you don’t always have to look at everything at once.

Also: you are welcome to set your IDE to auto expand. Bam!, problem solved for you. You will never ever be bugged by those hidden pieces of code again. Other people on your team, however, do not have an easy way to fold the code away if they wish to do so and you removed all the #region’s. Clearly, no #region’s hurt part of the team, and lots of #regions don’t really hurt anyone (since you can just set your IDE to auto expand if you don’t like them). The choice is absolutely obvious.


Jeff, you’ve been reading my blog again!

No to #region


Hmm… I have the answer, never use C#. There is no reason for it to exist. As for regions in VB, well VB is so verbose you need regions just to find your code.
I must admit to being a Java coder, so I have biases. I will have to say that I wish that Java had an answer to VS2008. Eclipse sucks, NetBeans makes the programmer code the way it wants you to code, and JBuilder’s libraries are out of date. So while MS languages are horrid, their IDE is still better than anyone else. Go TextPad.


Ctrl-M Ctrl-P

What are you, George Jetson? Waaah, Jane, my fingers hurt from pressing 2 buttons.

Or just turn it off in your IDE.

Seriously, call the Wahbulance.

Everyone thinks differently and I’m sure it really helps other people have a framework to get organized. Do I think people should group methods by private and public? No. But I’m not going to write a whiny blog post about it. (I just hit Ctrl-M Ctrl-P and it all goes away…)

And it beats the heck off 100 separate partial class files. Yuck! And there’s no way for people that don’t like that to fix it.