Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Code folding
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Use== Code folding has various [[use pattern]]s, primarily organizing code or hiding less useful information so one can focus on more important information. Common patterns follow.{{sfn|Atwood|2008}} ===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: <syntaxhighlight lang=java> private String name = null; public String getName() { return name; } public void setName(String name) { this.name = name; } </syntaxhighlight> This expands to 10 lines with conventional function line breaks and spacing between functions (including trailing newline): <syntaxhighlight lang=java> private String name = null; public String getName() { return name; } public void setName(String name) { this.name = name; } </syntaxhighlight> Documentation with Javadoc expands this to 20 lines: <syntaxhighlight lang=java> /** * 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; } </syntaxhighlight> 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 [[Attribute (computing)|attributes]] in [[C Sharp (programming language)|C#]] may be manually collapsed as follows:<ref>{{Cite web |last=Rob |date=March 19, 2014 |title=Now, this is interesting, because I tend to use #region more to HIDE THE CRAP (XML documentation etc, long list of attributes etc.) making it easier to see the important codeβ¦ |url=http://discourse.codinghorror.com/t/the-problem-with-code-folding/294/31 |url-status=live |archive-url=https://web.archive.org/web/20200806132848/https://discourse.codinghorror.com/t/the-problem-with-code-folding/294/31 |archive-date=Aug 6, 2020 |website=The Problem With Code Folding |publisher=Coding Horror Discussion}}</ref> <syntaxhighlight lang="csharp"> #region Attributes [Browsable(false)] [MergableProperty(false)] [DefaultValue(null)] [PersistenceMode(PersistenceMode.InnerProperty)] [TemplateContainer(typeof(MyType))] [TemplateInstance(TemplateInstance.Single)] #endregion public ITemplate ContentTemplate { get; set; } </syntaxhighlight> The resulting code displays as: <syntaxhighlight lang="csharp"> Attributes public ITemplate ContentTemplate { get; set; } </syntaxhighlight> ===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 generator]]s, such as [[Javadoc]] or [http://msdn.microsoft.com/en-us/library/b2s063f7.aspx 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 (computing)|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 <code>if...then...else</code>, 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 ===
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)