The Problem With Code Folding

When you join a team, it's important to bend your preferences a little to accommodate the generally accepted coding practices of that team. Not everyone has to agree on every miniscule detail of the code, of course, but it's a good idea to dicuss it with your team and decide on overall approaches and philosophy beforehand. It promotes team harmony, and more than that, it's just common courtesy. As they say, when in Rome, do as the Romans do.


This is a companion discussion topic for the original blog entry at: http://www.codinghorror.com/blog/2008/07/the-problem-with-code-folding.html

Finally someone who hates this useless #region crap. I have the same opinion - it drives me crazy when Iā€™m looking at someoneā€™s code and canā€™t see a thing. Linus Torvalds once wrote: if you need more than 3 levels of indentation, youā€™re screwed anyway. If you need to use #refion - see above :slight_smile:

@Non sequitur

Haha, Perfect

Like any new tool or technique, there is bound to be overuse and abuse until a consensus is attained about what constitutes a ā€œbest practiceā€. This discussion is part of the process of that consensus.

Personally, I admit to ā€œhidingā€ my exception handling within regions. This allows the flow of the program to read unimpeded by exceptional occurrences.

I donā€™t have quite the problem with code folding you do ā€“ I think that like most things in ā€˜programmingā€™ it can be used sparingly and make things easier for someone else to follow/read, or can be way overused and make things more of a PITA (like the way I feel about you and your 'varā€™ificationā€¦ ;-).

In the end I find myself using less #regions and more bricks/patterns of whitespace (lines) between logical chunks of code.

When I first heard of XMLDoc I thought it could be kind of cool and helpful, but the actual usage of it really pisses me off. It creates unnecessary junk - which to me says I am a really bad programmer - good programmers donā€™t create unnecessary junk.

I saw this today:
ā€™ā€™ā€™

#region Comment

// Coding Standard: C# Philips Medical Systems - Software / SPI
// 4.2.5. Rec. 4@110: Use #region to group non-public membersā€¦ 19

#endregion

Whenever I come across someone whom is extremely opinionated about coding style and intent on telling everyone else how they should indent or how they should name their variables, it usually is to compensate for the fact that they canā€™t read code to save their lives.

Being able to sit down and read someone elseā€™s code is a skill that takes time to develop. Unfortunately, people try to mask the fact they cannot with such annoying antics.

Wait until an engineer throws some FORTRAN 77 at you. Any non-modern Fortran code is guaranteed to look strange to a modern programmer. What is the coding prima donna going to do then? Tell them to redo it all in Java or F#?

region comment is very useful.

oh perfectly written is a myth. Unless you write the whole thing your self, or you spend couple of years in the hiring process.

unfortunately, programmers, developers, team leads, who ever, write crap and at one point someone else have to foot the bill and maintain this crap. Region makes the process less painful.

I have to echo Darren and Mark on this - Regionerate + ReSharper = useful regions. I tend to leave them expanded all the time and just use them for code organization, not folding.

http://www.rauchy.net/regionerate/
http://www.jetbrains.com/resharper/

@Non sequitur

I know the purpose of your post is to make light of the article. However you are close to showing why Regions suk.

In the book analogy one thing is clear, you did not reorganize chapters the way that some bad programmers rearrange classes within regions.

Your comment is facetious when merely discussing books and chapters.

However if you then reorganized your chapters by Nouns or some other arbitrary way. Youā€™re comment would no longer be humorous, instead it explains why regions suk.

Imagine reading Harry Potter in some arbitrary order of chapters, first all indoor scenes then all outdoor scenesā€¦(public/private)

Are you seeking attention?

This is a new language feature? hahahahahahahaha

// {{{ Rant

Vim has had code folding with markers like forever :slight_smile:

// }}}

/* vim: set foldmethod=marker: */

I saw a badly named variable the other day - God variables suck.

Another very good thought from codinghorror!

Folding also adds another thing that has to be maintained. We used it in a few areas in Paint.NET at the beginning, and over time it just fell apart. I find that the automatic - and + in the margin in Visual Studio gives me enough control and granularity for letting me hide code when I need to.

For those that donā€™t like regions (or automatic outlining in general), VS has a preference to turn it off.

Tools Options Text Editor C# Advanced Outlining

Simply uncheck the Enter outlining mode when files open box, and this discussion is over.

I personally love regions, not for grouping like members (e.g. public properties, private methods), but for grouping like functionality in a class. Makes finding the part of the code you need to work on much easier.

I believe Eclipse, Geany, and Zend Studio offer automatic folding. With options on how to work it as well.

I wish there was automatic blog-comments folding, so I didnā€™t need to scroll 50 screen to get to the form :wink:

Regarding actual folding, I believe it is a semi-smell that too much is going on in the same file. When you need to start folding, maybe itā€™s time to refactor and reorganize your actual code.

Your project is now full of crappy code that you canā€™t see

I want to have this framed and put on the wall at work :slight_smile:

Daniel