The Problem With Code Folding

@Jeff

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.

Jeff,

100% agree. #regions blow.

Might as well have called it

#rugwithdustunderneath
#loadedgun
#tomaximizeyourcodingtimeandeffortlookherelast

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.
i.e.
describe MyClass do
it should do something do
(ā€¦folded)

it should do something else do
(ā€¦folded)

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

describe when not doing something do
(ā€¦folded)
end

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
http://mikehadlow.blogspot.com/2006/10/no-to-region.html

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.