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
Indentation style
(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!
== Losing track of blocks == {{more citations needed|section|date=March 2024}} In more complicated code, the programmer may lose track of block boundaries while reading the code. This is often experienced in large sections of code containing many compound statements nested to many levels of indentation. As the programmer scrolls to the bottom of a huge set of nested statements, they may lose track of context {{endash}} such as the control structure at the top of the block. Long compound statements can be a [[code smell]] of [[Cyclomatic complexity|over complexity]] which can be solved by [[refactoring]]. Programmers who rely on counting the opening braces may have difficulty with indentation styles such as K&R, where the starting brace is not visually separated from its [[Control flow|control statement]]. Programmers who rely more on indentations will gain more from styles that are vertically compact, such as K&R, because the blocks are shorter. To avoid losing track of control statements such as <code>[[for loop|for]]</code>, a large indentation can be used, such as an 8-unit-wide hard tab, along with breaking up large functions into smaller and more readable functions. Linux is done this way, while using the K&R style. Some text editors allow the programmer to jump between the two corresponding braces of a block. For example, [[Vi (text editor)|vi]] jumps to the brace enclosing the same block as the one under the cursor when pressing the <code>%</code> key. Since the text cursor's <code>next</code> key (viz., the <code>n</code> key) retained directional positioning information (whether the <code>up</code> or <code>down</code> key was formerly pressed), the [[Macro (computer science)#Keyboard and mouse macros|dot macro]] (the <code>.</code> key) could then be used to place the text cursor on the next brace,<ref>{{cite book |first=Linda |last=Lamb |title=Learning the vi editor |year=1998 |url=https://archive.org/details/learningvieditor00lamb |url-access=registration |publisher=O'Reilly|isbn=9781565924260 }}</ref> given a suitable coding style. Instead, inspecting the block boundaries using the <code>%</code> key can be used to enforce a coding standard. Another way to maintain block awareness, is to use comments after the closing brace. For example: <syntaxhighlight lang=c> for (int i = 0; i < total; i++) { foo(); } //for (i) </syntaxhighlight> <syntaxhighlight lang=c> if (x < 0) { bar(); } //if (x < 0) </syntaxhighlight> A disadvantage is maintaining the same code in multiple locations {{endash}} above and below the block. Some editors provide support for maintaining block awareness. A [[folding editor]] can hide (fold) and reveal (unfold) blocks by indentation level. Some editors highlight matching braces when the [[Cursor (user interface)|cursor]] is positioned next to one.
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)