Code or text folding, or less commonly holophrasting, is a feature of some graphical user interfaces that allows the user to selectively hide ("fold") or display ("unfold") parts of a document. This allows the user to manage large amounts of text while viewing only those subsections that are currently of interest. It is typically used with documents which have a natural tree structure consisting of nested elements. Other names for these features include expand and collapse, code hiding, and outlining. In Microsoft Word, the feature is called "collapsible outlining".

Many user interfaces provide disclosure widgets for code folding in a sidebar, indicated for example by a triangle that points sideways (if collapsed) or down (if expanded), or by a [-] box for collapsible (expanded) text, and a [+] box for expandable (collapsed) text.

Code folding

Code folding is found in text editors, source code editors, and IDEs. The folding structure typically follows the syntax tree of the program defined by the computer language. It may also be defined by levels of indentation, or be specified explicitly using an in-band marker (saved as part of the source code) or out-of-band.

Code folding

Text folding is a similar feature used on ordinary text, where the nested elements consist of paragraphs, sections, or outline levels. Programs offering this include folding editors, outliners, and some word processors.

Code folding

Data folding is found in some hex editors and is used to structure a binary file or hide inaccessible data sections.

Folding is also frequently used in data comparison, to select one version or another, or only the differences.

History

The earliest known example of code folding in an editor is in NLS. Probably the first widely available folding editor was the 1974 Structured Programming Facility (SPF) editor for IBM 370 mainframes, which could hide lines based on their indentation. It displayed on character-mapped 3270 terminals. It was very useful for prolix languages like COBOL. It evolved into the Interactive System Productivity Facility (ISPF).

Use

Code folding has various use patterns, primarily organizing code or hiding less useful information so one can focus on more important information. Common patterns follow.

Outlining

Most basically, applications use code folding to outline source code, collapsing each block to a single line. This can be only top-level blocks like functions and classes, nested blocks like nested functions and methods, or all blocks, notably control-flow blocks. This allows one to get an overview of code, easily navigating and rearranging it, and to drill down into more detail as needed, without being distracted by other code. Viewing-wise, this allows one to quickly see a list of all functions (without their bodies), while navigation-wise this replaces extensive paging past long functions – or searching for the target – with going directly to the next function.

Hiding boilerplate code

Some languages or libraries require extensive boilerplate code. This results in extremely long code, which can obscure the main point. Further, substantive code can be lost in the boilerplate.

For example, in Java a single private field with a getter and setter requires at least 3 lines, if each is on a separate line:

private String name = null; public String getName() { return name; } public void setName(String name) { this.name = name; }

This expands to 10 lines with conventional function line breaks and spacing between functions (including trailing newline):

private String name = null;

public String getName() {

return name; }

public void setName(String name) {

this.name = name; }

Documentation with Javadoc expands this to 20 lines:

/**

* Property <code>name</code> readable/writable. */ private String name = null;

/**

* Getter for property <code>name</code>

*/

public String getName() {

return name; }

/**

* Setter for property <code>name</code>. * @param name */ public void setName(String name) { this.name = name; }

If there are many such fields, the result can easily be hundreds of lines of code with very little "interesting" content – code folding can reduce this to a single line per field, or even to a single line for all fields. Further, if all routine fields are folded, but non-routine fields (where getter or setter is not just returning or assigning a private field) are not folded, it becomes easier to see the substantive code.