ArticleslgStudy

science

Code folding

Code folding is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Code folding rather than just read about it. In short: 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.

Code folding — main illustration
Code folding — illustration

Key takeaways

  • Code folding belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Code folding to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Code folding from memory before moving on to harder problems.

Reference excerpt

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 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. 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. 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:

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

Documentation with Javadoc expands this to 20 lines:

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:

The resulting code displays as:

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

… excerpt ends here. Continue reading the full article.

Illustrations

Code folding: Code folding example on PHP code with Vim
Code folding example on PHP code with Vim

Worked examples

Example 1 — a first encounter with Code folding

Start with the simplest possible case. Write down what Code folding claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Code folding before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Code folding ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Code folding

In research
Code folding appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Code folding in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Code folding is common in secondary-school and first-year university syllabi. It links to neighbouring topics Source code, Text editor features, so understanding it makes those chapters shorter.
In everyday life
Look for Code folding outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Code folding” →

How to study Code folding in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Code folding means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Code folding out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Code folding in simple terms?

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 curren…

Why does Code folding matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Code folding?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Code folding.

Tags

  • Source code
  • Text editor features

Keep exploring