What is Code Folding
Code folding allows to hide a group of lines in a text file and to show
only a descriptive headline instead. This allows to hide the details and
to show the structure of a text file, like the summary of a book.
Folding methods
There is several methods to specify which lines are to be folded.
1. Syntax-based folding
The editor analyses the source code and selects
the lines that can be folded, usually it allows to fold classes, functions,
if-then-else statements, ...
Syntax-based folding tends to create too many folds without
added value: folding every 'if' and 'while' blocks does not help navigation
or readability, except may be, huge 'while' blocks, but anyway, creating
such blocks is a very bad programming practice.
2. Indentation-based folding
The indented blocks of code can be folded.
3. Marker-based folding
Also referred to as explicit folding in jEdit.
The editor allows to insert markers in the text file to delimit the beginning and the end of the
section of text to hide. These markers are usually embedded into comments to prevent
a compiler to interpret them.
This method is the most flexible, you can fold any
group of lines and specify any descriptive headline instead of just showing the first line.
It also works with any kind of source file allowing comments, and fortunately, it is the
case with almost all programming languages and markup languages.
But this method is also the most painful to use because you must select manually the
regions of text to be folded and enter a headline for each one. However the extra effort worth it.
Navigation methods
When some text is folded in a folding editor, there is two way to get it.
1. Expanding
The collapsed section is expanded, so you can edit text and collapse the
section again when finished. Collapsing and Expanding is usually performed
with shortcuts or a button in the left margin.
A 'while' block collapsed in SciTE, and the block once expanded.
This is the most common method used by almost 99% of text
editors having folding capability.
2. Changing context
Instead of collapsing and expanding sections, this method keeps always folded section
collapsed and if you want to view such a section, the current text is replaced by the collapsed
text.
This approach transforms a long flat text file into a hierarchy of smaller text blocks.
Example
This is a small example in C...
#include <stdio.h>
int fact(int n)
{
if( n < 2 )
return 1;
else
return n * fact(n-1);
}
int main()
{
int n = 5;
printf("fact(%d) = %d\n", n, fact(n));
return 0;
}
The fact() functions is now folded with just a headline displayed.
The implementation detail of the function is hidden an there is only a descriptive
headline visible.
#include <stdio.h>
fact(n) - Factorial
int main()
{
int n = 5;
printf("fact(%d) = %d\n", n, fact(n));
return 0;
}
There is markers in comments at the beginning and at the end of the
function, so, for the compiler, it is a perfectly valid source code.
#include <stdio.h>
//[of]:fact(n) - Factorial
int fact(int n)
{
if( n < 2 )
return 1;
else
return n * fact(n-1);
}
//[cf]
int main()
{
int n = 5;
printf("fact(%d) = %d\n", n, fact(n));
return 0;
}
Folding in Code Browser
Code Browser supports only the marker-based folding. This method is far
more flexible than indentation-based or syntax-based folding but it requires
more manual work by the user.
Almost all text editors that implement folding use a collapse and
uncollapse method to show the folded content. Code Browser uses a different
mechanism, the content is displayed in a different page: you can 'enter' or
'leave' a folded section. When there is only one page, the current page is
replaced by the new one, and it is possible to return to the previous page
with the 'Back' command.
The behavior is very similar to navigation on the WEB with a browser.

Root of the text file
|

Entering into a folded section is like following a link in a web page.
|
Code Browser can show the same structured text file with different layouts.

Two panes: the second pane shows the content of the folded line
selected in the first pane
|

Smalltalk style
|

Tree view: the tree shows the hierarchy of all folded sections
|
Advantages of Folding
The code structure is easier to view
A block of code, a function or a class for instance can be sumarized to a single headline.
Faster navigation
No need to scroll page after page to find something, you navigate through a tree instead
of a long flat file.
Scope of search
Search and replace can be limited to a particular folded section.
Better programming practices
It encourages better structuration of code and better commenting.
See also
|