The Problem With Code Folding

I sometimes feel it useful to fold code at the entity level (class, method) but I really hate the profusion of #region in the c# code

For code browsing it is much easier to use a free-spinning-wheel mouse (like the Logitech VX) to cruise through the code and stop with a fingertip touch when needed.

Iā€™m sure there are sane uses for code folding out there somewhere, but I rarely see them

I agree, iā€™ve rarely seen regions used helpfully, visual studio will let you fold up methods quite happily, in most cases if your class has too many methods to fit, one per line, on the IDE then you have done something very very wrong.

Sameer Alibhai: dude, whats with the comment about terrorists?

I was wondering the same thing.

I disagree with this article. Regioning is just another way of organising your code and of limiting the amount of noise your brain has to deal with when looking at a file (noise being defined as anything you arenā€™t immediately interested in).

When you open a large source file and see slews of XML comments and code everywhere, your brain expends unconscious energy deciding that itā€™s all irrelevant. When you open up to some nicely collapsed regions, that energy is saved and you can quickly find the relevant section of code. Repeat this process 500 times in a day and the needless energy expenditure quickly piles up.

Of course, benefiting from regions depends on their sensible use. Having regions like public methods, static methods, private methods is about as useful as a barrel full of pig shit. Like any other tool it needs to be used well to be beneficial.

Regioning makes files easier to navigate and information easier to partition into bite-sized chunks that your brain can more easily process without getting tired from sorting through a sea of green, black and blue text.

I think youā€™re off the deep end on this one.

I love #regions, but - just like yourself - think that coders use them to group constructors, methods, properties, etc. should be ritually and publicly shot in the town square immediately. ARGH!

I try to use them as sparingly as possible, but when I do, I love them. One example: real world class that has some feature, implemented with a public method/property and a number of private member variables and/or methods/properties. I then functionally group that functionality into one #region. When the region gets too big, itā€™s a prime suspect for refactoring in a seperate class. Then they really add a lot.

But I agree that putting all your code in useless and often multiple #region subdivisions is about as evil as evil code gets! Too bad of an otherwise potentially great featureā€¦

I hate it when you implement the interface in Visual studio and it auto-adds regions around the generated code. And when you add the interface and click it again, you get yet another region. Bad.

I usually agree with Jeff. I also like that he encourages and expects disagreement.

That said, I think this entry is somewhat shrill. I find #regions helpful in organizing code and allowing me to focus on particular sections. Any tool can be used irresponsibly, so I donā€™t think #regions are necessarily appropriate, but I do like the idea that I can group functions together, that perhaps I donā€™t wish to factor into a new class.

Regions ROCK!!!
Go back to vb6 and than youā€™ll realize why we have regions.

Code folding should be done by the IDE - It already understands the code so can fold it the same as it can highlight/colour/complete it ā€¦

If your code is so long it needs folding to be manageable then you should not use folding ā€¦

It is a nice feature to hide things that are currently not needed so you donā€™t have to keep scrolling/searching around the code

The region directives if not understood by the compiler should not be in the code they are glorified comments

If youā€™re using Intellisense, it un-collapses code. Unless Iā€™m in the process of constructing the class, I very rarely go into a file without getting there from Intellisense.

I used to not be a big fan of the regions; but used properly they can save you some time. If nothing else; if you need to scroll back and forth between some sections of code, by hiding code that you donā€™t need to see it can make it a little easier.

I do agree with you that VS should have the ability to sort/group code based on protection type; similar to how it can sort using statements. On a similar note; I hate that VS canā€™t create properties if you select more that one private memberā€¦ why can it only do one at a time?

#regions, used in moderation, are a useful organizing tool.

Jeff, next youā€™ll be telling us how much you loathe chapters in books, or separate entries for blog posts, each comment in itā€™s own blockā€¦

I actually like code folding, it keeps un-necessary code out of sight and allows you to focus solely on the section you are interested in. folding on code blocks makes sense but some times say you have a class extending more than one interface (and this happens a lot) I find it really clever to be able to fold up implementations for distinct interfaces in distinct segments. If #regions are a waste of space, i think comments are an even greater waste. and to extend that logic, any coder worth his/her salt should be able to understand code and not need a comment. But of course some like me need it any way.

Sorry - Code Folding is awesome. I wrote a patch for it in Vim.

You have to remember that the mental state of the programmer is the most important thing in programming, and being able to see everything you need on screen at once, and being able to fold away the unimportant parts, is critical.

Maybe how VS does it is crap, I donā€™t know. But in Vim, my patch would allow you to search for a term (say, foobar-wizwoz) and then fold away all the intervening lines, with a certain number of lines of context around them. This allowed amazingly fast code refactoring, and for complex refactoring operations it allowed you to see all of your other changes all in one glance, which made it both faster and less error-prone.

Was a grad school project, and something I was probably the most proud of. Can be downloaded here:
http://www-cse.ucsd.edu/~wkerney/vim_bill.tar.gz

Readme here:
http://www-cse.ucsd.edu/~wkerney/vim_bill.README

I think we all have seen the overuse of regions in codebases we maintain. I am in complete agreement that the automatic use of regions in every file is a code smell and should be avoided. Regions should be used just like comments, only when they make the code clearer or easier to read/understand.

For people looking to make their code more readable with regions I suggest reading Robert C. Martin new book Clean Code: A Handbook of Agile Software Craftsmanship. It is in rough cuts on Oā€™Reilly Safari.

PS no regions necessary.

Errrā€¦ I use regions in the VB.NET Express IDE and it does NOT automatically fold them closed every time I open up a file.

Is this perhaps only behavior found in Visual Studio Professional? Or is my computer just charmed to do things the right way?

Oh, and my $0.02 - I use regions, but sparingly, in classes. Itā€™s nice to be able to unfold just the particular section Iā€™m after, and to fold related functions/declarations/etc. out of the way when Iā€™m not using them. Especially when things get really long. The way the IDE shows folded code is visually distinct enough that I wonā€™t forget to unfold something. So, I find it helpful and useful, though I donā€™t use it a lot.

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.

Microsoft doesnā€™t negotiate with programmers! Buy VS 2008ā€¦suckers.

I think regions are OK for hiding and grouping code, so long as theyā€™re not abused to make nonsense groups. If your regions have names like private fields, constructors and public properties, please donā€™t do that. Such groupings are not logical, and actually preclude any kind of logical grouping. It increases maintenance effort since people now have to add related things in different areas of the file; either that or they donā€™t bother, making even more of a mess.

funny, i agree completly.

You have just forgotten one point. The standard search in Visual Studio does not search in hidden parts, like regions. (o.k. you can set a flag to search in hidden code, but that is not the standard behaviour, and i was very confused when a new programmer had changed my code and yust put regions in it after we worked 2 years completly without regions. I searched for something in my code and didnā€™t find it in first place. Another 5 Minutes wasted when lookin why i canā€™t find my code in my class. First looked in our source control system if anyone has changed namesā€¦ and only seeing added region en masse.)

Iā€™m going to have to disagree with you on this one Jeff.

In my experience itā€™s been a very useful tool in the VS IDE. Iā€™m not sure if any other editors support it, but it seems like youā€™re railing against a feature of single (albeit major) IDE.

I think programming should be about elegant, efficient coding, using the syntactical structure of the chosen language. The #region tag is a tool useful for visual studio, but it means nothing to notepad. It does however still provide a visual cue as to what a certain block of code might do. Can it be abused? Sure, but with some discipline I believe it can be used to great effect.

Donā€™t lose sight of what youā€™re trying to accomplish and the code youā€™re trying to write by getting caught up in the features of an IDE that as a whole can quickly become a crutch.