Monday, 22 December 2008

Program Structure – Code Folding

You know how in Delphi 2005 (could have been Delphi 8, but who actually used that?) Object Pascal was introduced to the world of code folding. Code folding, is where the IDE lets you hide certain parts of your code. Either it automatically chooses certain areas, or you can add further areas of your own.

Great, we all said. We have the same feature they have in C# and Visual Studio. Honestly though, have you ever used it in Delphi? I did initially, just because it was there, but I soon realised I just didn’t need it. Object Pascal with it’s interface and implementation sections allows you to look at a class’s declaration, and understand what it does at a glance. C# on the other hand, mingles the declaration with the implementation, which makes classes unreadable if they get above a certain size.

You can look at this from two angles.

  1. Object Pascal, while verbose is much more readable. With things like class completion and jumping between method declarations and implementation, the verbosity is not that big a deal, and anyway, I’d rather type a little more code if it makes it more readable.
  2. C# encourages you to make classes small. That’s a good thing. With Object Pascal, you can create very large classes, and still maintain readability, whereas with C#, you are forced early on to keep classes small.

So I guess the conclusion I came up with is that code folding is bad. If you need to fold code to make it readable, then your classes are probably too big. If you ignore code folding, C# actually may have an advantage here, because you are forced to some extent to keep classes small, whilst Object Pascal encourages (or at least doesn’t penalise) you to use large classes.

I think I’m going to call it a draw!

7 comments:

Birger Jansen said...

What I find a rather big problem with code folding is that the IDE does not remember what parts of the code I have folded (at least, I couldn't find this option in D2006 / D2007). If you close a unit and re-open it, al the code is unfolded again. Makes it useless for me!

Thomas Mueller said...

I too have to admit it: I wanted code folding very bad, but when I got it, I never used it. The main reason, though, is my use of a code formatter. Every time I do a reformat, code folding gets turned off again.

Alexander said...

Nice thoughts!

Lars D said...

I could not agree more. I almost forgot that code folding exists...

Alister Christie said...

I don't think there is anything wrong with code folding, although I've never really used it much. What is probably a bad practice is defining regions where you can do something like
{$region 'bad code'}
//something that you hope no one else notices
{$endregion}

You can also toggle code folding on and off by pressing CTRL+SHIFT+K, CTRL+SHIFT+O
I demonstrate this in my 100 IDE Hints and Tips video I did for CodeRage III which you can view on my website.

Lachlan said...

{$REGION 'Assert'}Assert(x = y, 'Some distractingly long message.');{$ENDREGION}

Single line code regions, I've grown addicted to them for Asserts and logging statements.

MarkF said...

Is it necessary? No! Do I find it useful and actually use it? Yes!

A note to Birger:
I believe that the code folding saving is tied to the "Autosave Options" "Project Desktop" setting. I use this option and my code folding is always preserved.