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 name readable/writable. */ private String name = null; /** * Getter for property name */ public String getName() { return name; } /** * Setter for property name. * @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.
Collapsing metadata Metadata can be lengthy, and is generally less important than the data it is describing. Collapsing metadata allows one to primarily focus on the data, not the metadata. For example, a long list of
attributes in
C# may be manually collapsed as follows: • region Attributes [Browsable(false)] [MergableProperty(false)] [DefaultValue(null)] [PersistenceMode(PersistenceMode.InnerProperty)] [TemplateContainer(typeof(MyType))] [TemplateInstance(TemplateInstance.Single)] • endregion public ITemplate ContentTemplate { get; set; } The resulting code displays as: Attributes public ITemplate ContentTemplate { get; set; }
Collapsing comments Comments are a form of human-readable metadata, and lengthy comments can disrupt the flow of code. This can be the case either for a long comment for a short section of code, such as a paragraph to explain one line, or comments for
documentation generators, such as
Javadoc or XML Documentation. Code folding allows one to have long comments, but to display them only when required. In cases where a long comment has a single summary line, such as Python docstrings, the summary can still be displayed when the section is collapsed, allowing a summary/detailed view.
Showing structure or sandwich code in structured programming Structured programming consists of nested blocks of code, and long blocks of code – such as long switch statements – can obscure the overall structure. Code folding allows one to see the overall structure and expand to a specific level. Further, in some uses, particularly strict structured programming (single function exit), there are code patterns that are hard to see when looking at expanded code. For example, in
resource management in structured programming, one generally acquires a resource, followed by a block of code using the resource, and finishing with releasing the resource. The acquisition/release pairing is hard to see if there is a long block of code in between, but easy to see if the intervening block is folded. Similarly, in conditional code like if...then...else, secondary blocks may be far from the condition statement.
Grouping code Fold groups can be used to group code, either by explicit grouping – similar to comment blocks separating a module into sections, or class members into associated groups – or implicitly, such as by automatically grouping class members by access level.
Hiding legacy code Legacy code – or any code that a developer does not wish to view or change at a given point in time – can be folded away so that programmers can concentrate on the code under consideration.
Hiding in-source data tables ==Conventions==