Indent style
Encyclopedia
In computer programming
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...

, an indent style is a convention governing the indentation
Indentation
An indentation may refer to:* A notch, or deep recesses; for instance in a coastline, or a carving in rock* The placement of text farther to the right to separate it from surrounding text....

 of blocks of code to convey the program's structure. This article largely addresses the C programming language
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 and its descendants, but can be (and frequently is) applied to most other programming languages (especially those in the curly bracket family). Indent style is just one aspect of programming style
Programming style
Programming style is a set of rules or guidelines used when writing the source code for a computer program. It is often claimed that following a particular programming style will help programmers to read and understand source code conforming to the style, and help to avoid introducing errors.A...

.

Indentation is not a requirement of most programming languages. Rather, programmers indent to better convey the structure of their programs to human readers. In particular, indentation is used to show the relationship between control flow
Control flow
In computer science, control flow refers to the order in which the individual statements, instructions, or function calls of an imperative or a declarative program are executed or evaluated....

 constructs such as conditions or loops and code contained within and outside them. However, some programming languages (such as Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

 and Occam) use the indentation to determine the structure instead of using braces or keywords.

The size of the indent is usually independent of the style. Many early programs used tab
Tab key
Tab key on a keyboard is used to advance the cursor to the next tab stop.- Origin :The word tab derives from the word tabulate, which means "to arrange data in a tabular, or table, form"...

 characters for indentation, for simplicity and to save on source file size. Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 editors generally view tabs as equivalent to eight characters, while Macintosh and Microsoft Windows
Microsoft Windows
Microsoft Windows is a series of operating systems produced by Microsoft.Microsoft introduced an operating environment named Windows on November 20, 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces . Microsoft Windows came to dominate the world's personal...

 environments would set them to four, creating confusion when code was transferred back and forth. Modern programming editors are now often able to set arbitrary indentation sizes, and will insert the appropriate combination of spaces and tabs. For Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

, many shell programming languages
Shell script
A shell script is a script written for the shell, or command line interpreter, of an operating system. It is often considered a simple domain-specific programming language...

, and some forms of HTML
HTML
HyperText Markup Language is the predominant markup language for web pages. HTML elements are the basic building-blocks of webpages....

 formatting, two spaces per indent level is generally used.

The topic issue of using hard tabs or spaces is an ongoing debate in the programming community. Some programmers such as Jamie Zawinski
Jamie Zawinski
Jamie Zawinski , commonly known as jwz, is a former professional American computer programmer responsible for significant contributions to the free software projects Mozilla and XEmacs, and early versions of the Netscape Navigator web browser...

 feel that spaces instead of tabs increase cross-platform functionality. Others, such as the writers of the WordPress
WordPress
WordPress is a free and open source blogging tool and publishing platform powered by PHP and MySQL. It is often customized into a content management system . It has many features including a plug-in architecture and a template system. WordPress is used by over 14.7% of Alexa Internet's "top 1...

 Coding Standards, believe the opposite, that hard tabs increase cross-platform functionality.

There are a number of computer programs that automatically correct indent styles as well as the length of tab
Tab
Tab or tabs may refer to:* Tab, a British Army term for a loaded march* Tab , by Monster Magnet* Tab , a small protective covering for the fingers* Tab , the mechanism for opening a beverage can...

s. A famous one among them is indent
Indent (Unix)
indent is a Unix utility that reformats C and C++ code in a user-defined indent style and coding style. Support for C++ code is considered experimental.-Examples of usage:The command line...

, a program included with many Unix-like
Unix-like
A Unix-like operating system is one that behaves in a manner similar to a Unix system, while not necessarily conforming to or being certified to any version of the Single UNIX Specification....

 operating system
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...

s. These programs work best for those who use an indent style close to that considered "proper" by their programmers; those who use other styles will more likely become frustrated. Furthermore, indent has only been updated once in the past 6 years and does not work well either with C++ or GNU extensions to C.

K&R style

The K&R
The C Programming Language (book)
The C Programming Language is a well-known programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as co-designed the Unix operating system with which development of the language was closely intertwined...

 style, so named because it was used in Kernighan
Brian Kernighan
Brian Wilson Kernighan is a Canadian computer scientist who worked at Bell Labs alongside Unix creators Ken Thompson and Dennis Ritchie and contributed to the development of Unix. He is also coauthor of the AWK and AMPL programming languages. The 'K' of K&R C and the 'K' in AWK both stand for...

 and Ritchie's
Dennis Ritchie
Dennis MacAlistair Ritchie , was an American computer scientist who "helped shape the digital era." He created the C programming language and, with long-time colleague Ken Thompson, the UNIX operating system...

 book The C Programming Language
The C Programming Language (book)
The C Programming Language is a well-known programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as co-designed the Unix operating system with which development of the language was closely intertwined...

, is commonly used in C. It is less common for C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

, C#, and others.

When adhering to ANSI C each function has its opening brace at the next line on the same indentation level as its header, the statements within the braces are indented, and the closing brace at the end is on the same indentation level as the header of the function at a line of its own. The blocks inside a function, however, have their opening braces at the same line as their respective control statements; closing braces remain in a line of their own, unless followed by an else or while keyword.

In this style a control statement with only a single statement in its scope may omit the braces. The C Programming Language refers to this as fertile soil for bugs (programming logical errors) and discourages it.


int main(int argc, char *argv[])
{
...
while (x y) {
something;
somethingelse;

if (some_error) {
/* the curly braces around this code block could be omitted */
do_correct;
} else
continue_as_usual;
}

finalthing;
...
}


In old versions of the C programming language, the functions, however, were braced distinctly. The opening function brace of a function was placed on the line following after the declaration section and at the same indentation level as the declaration (header of the function). This is because in the original C language, argument types needed to be declared on the subsequent line (i. e., just after the header of the function), whereas when no arguments were necessary, the opening brace would not appear in the same line with the function declaration. The opening brace for function declarations was an exception to the currently basic rule stating that the statements and blocks of a function are all enclosed in the function braces.


/* Original pre-ISO C style without function prototypes */
int main(argc, argv)
int argc;
char *argv[];
{
...
}

Variant: 1TBS

Advocates of this style sometimes refer to it as "The One True Brace Style" (abbreviated as 1TBS or OTBS) because of the precedent set by C (although advocates of other styles have been known to use similarly strong language). The source code of the Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 kernel was written in this style, as is the Linux
Linux
Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component of any Linux system is the Linux kernel, an operating system kernel first released October 5, 1991 by Linus Torvalds...

 kernel.

In this style, the constructs that allow insertions of new code lines are on separate lines, and constructs that prohibit insertions are on a single line. This principle is amplified by bracing every if, else, while, etc.—even single-line conditionals—so that insertion of a new line of code anywhere is always "safe" (i.e., such an insertion will not make the flow of execution disagree with the source code indentation).

Advantages of this style are that the beginning brace does not require an extra line by itself; and the ending brace lines up with the statement it conceptually belongs to. One disadvantage of this style is that the ending brace of a block takes up an entire line by itself, which can be partially resolved in if/else blocks and do/while blocks:


//...
if (x < 0) {
puts("Negative");
negative(x);
} else {
puts("Non-negative");
nonnegative(x);
}


While this style may make it difficult to scan any source code for the opening brace of a block, it is not usually the opening brace itself that is interesting, but rather the controlling statement that introduced the block. It is easy to find the beginning of the block by locating the first line above the closing brace which is indented to the same level.

While Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 is often written in Allman or other styles, a significant body of Java code uses a minor variant of the K&R style in which the opening brace is on the same line as the class or method declaration, largely because Sun
Sun Microsystems
Sun Microsystems, Inc. was a company that sold :computers, computer components, :computer software, and :information technology services. Sun was founded on February 24, 1982...

's original style guides used this K&R variant, and as a result most of the standard source code for the Java API
Java Class Library
The Java Class Library is a set of dynamically loadable libraries that Java applications can call at run time. Because the Java Platform is not dependent on any specific operating system, applications cannot rely on any of the existing libraries...

 is written in this style. It is also a popular indent style for ActionScript
ActionScript
ActionScript is an object-oriented language originally developed by Macromedia Inc. . It is a dialect of ECMAScript , and is used primarily for the development of websites and software targeting the Adobe Flash Player platform, used on Web pages in the form of...

 and JavaScript
JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

, along with the Allman style.

It should be noted that The C Programming Language does not explicitly specify this style, though it is followed consistently throughout the book. Of note from the book:

The position of braces is less
important, although people hold passionate beliefs. We have chosen one of several popular
styles. Pick a style that suits you, then use it consistently.



Allman style
The Allman style is named after Eric Allman
Eric Allman
Eric Paul Allman is an American computer programmer who developed sendmail and its precursor delivermail in the late 1970s and early 1980s at UC Berkeley.-Education and training:...

. It is sometimes referred to as "ANSI style" for its use in the documents describing the ANSI
Ansi
Ansi is a village in Kaarma Parish, Saare County, on the island of Saaremaa, Estonia....

 C
ANSI C
ANSI C refers to the family of successive standards published by the American National Standards Institute for the C programming language. Software developers writing in C are encouraged to conform to the standards, as doing so aids portability between compilers.-History and outlook:The first...

 standard (later adopted as the ISO C international standard). It is also sometimes known as "BSD style" since Allman wrote many of the utilities for BSD Unix
Berkeley Software Distribution
Berkeley Software Distribution is a Unix operating system derivative developed and distributed by the Computer Systems Research Group of the University of California, Berkeley, from 1977 to 1995...

 (although this should not be confused with the different "BSD KNF style"; see below). Proponents of this style often cite its use by ANSI and in other standards as justification for its adoption.

This style puts the brace associated with a control statement on the next line, indented to the same level as the control statement. Statements within the braces are indented to the next level.


while (x y)
{
something;
somethingelse;
}

finalthing;


This style is similar to the standard indentation used by the Pascal programming language and Transact-SQL
Transact-SQL
Transact-SQL is Microsoft's and Sybase's proprietary extension to SQL. SQL, often expanded to Structured Query Language, is a standardized computer language that was originally developed by IBM for querying, altering and defining relational databases, using declarative statements...

, where the braces are equivalent to the begin and end keywords.

Advantages of this style are that the indented code is clearly set apart from the containing statement by lines that are almost completely whitespace
White space (visual arts)
In page layout, illustration and sculpture, white space is often referred to as negative space. It is that portion of a page left unmarked: the space between graphics, margins, gutters, space between columns, space between lines of type or figures and objects drawn or depicted...

, improving readability, and the closing brace lines up in the same column as the opening brace, making it easy to find matching braces. Additionally, the blocking style delineates the actual block of code from the associated control statement itself. Commenting-out the control statement, removing the control statement entirely, refactoring, or removing of the block of code is less likely to introduce syntax errors because of dangling or missing brackets.

For example, the following is still syntactically correct:

//while (x

y)
{
something;
somethingelse;
}

As is this:

//for (int i=0; i < x; i++)
//while (x

y)
if (x y)
{
something;
somethingelse;
}


A disadvantage of this style is that each of the enclosing braces occupies an entire line by itself without adding any actual code. This once was an important consideration when programs were usually edited on terminals that displayed only 24 lines, but is less significant with the larger resolutions of modern displays. Since the motivation of this style is to promote code readability by visually separating blocks from their control statements, screen real estate is only a secondary concern.

This style is used by default in Microsoft Visual Studio
Microsoft Visual Studio
Microsoft Visual Studio is an integrated development environment from Microsoft. It is used to develop console and graphical user interface applications along with Windows Forms applications, web sites, web applications, and web services in both native code together with managed code for all...

 2005 and later versions. Microsoft has since then adopted the style throughout all of its documentation (MSDN) and internal programming methodologies for its C-based languages, namely C++ and C#.
BSD KNF style
Also known as Kernel Normal Form
Kernel Normal Form
Kernel normal form, or KNF, is the coding style used in the development of code for the BSD operating systems. Based on the original KNF concept from the Computer Systems Research Group, it dictates a programming style to which contributed code should adhere prior to its inclusion into the codebase...

 style, this is currently the form of most of the code used in the Berkeley Software Distribution
Berkeley Software Distribution
Berkeley Software Distribution is a Unix operating system derivative developed and distributed by the Computer Systems Research Group of the University of California, Berkeley, from 1977 to 1995...

 operating systems. Although mostly intended for kernel code, it is widely used as well in userland code. It is essentially a thoroughly-documented variant of K&R style as used in the Bell Labs Version 6 & 7 UNIX
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 source code.

The hard tabulator (ts in vi
Vi
vi is a screen-oriented text editor originally created for the Unix operating system. The portable subset of the behavior of vi and programs based on it, and the ex editor language supported within these programs, is described by the Single Unix Specification and POSIX.The original code for vi...

) is kept at 8 columns, while a soft tabulator is often defined as a helper as well (sw in vi), and set at 4.

The hard tabulators are used to indent code blocks, while a soft tabulator (4 spaces) of additional indent is used for all continuing lines which must be split over multiple lines.

Moreover, function calls do not use a space before the parenthesis, although C language native statements such as if, while, do, switch and return do (in the case where return is used with parens). Functions which declare no local variables in their top-level block should also leave an empty line after their opening block brace.

Here follow a few samples:


while (x y) {
something;
somethingelse;
}
finalthing;


 

if (data != NULL && res > 0) {
if (JS_DefineProperty(cx, o, "data",
STRING_TO_JSVAL(JS_NewStringCopyN(cx, data, res)),
NULL, NULL, JSPROP_ENUMERATE) != 0) {
QUEUE_EXCEPTION("Internal error!");
goto err;
}
PQfreemem(data);
} else {
if (JS_DefineProperty(cx, o, "data", OBJECT_TO_JSVAL(NULL),
NULL, NULL, JSPROP_ENUMERATE) != 0) {
QUEUE_EXCEPTION("Internal error!");
goto err;
}
}


 

static JSBool
pgresult_constructor(JSContext *cx, JSObject *obj, uintN argc,
jsval *argv, jsval *rval)
{

QUEUE_EXCEPTION("PGresult class not user-instanciable");

return (JS_FALSE);
}

Whitesmiths style

The Whitesmiths style, also called Wishart style to a lesser extent, is less common today than the previous three. It was originally used in the documentation for the first commercial C compiler, the Whitesmiths Compiler
Whitesmiths
Whitesmiths Ltd. was a software company based in Westford, Massachusetts. It sold a Unix-like operating system called Idris, as well as the first commercial C compiler...

. It was also popular in the early days of Windows, since it was used in three influential Windows programming books, Programmer's Guide to Windows by Durant, Carlson & Yao, Programming Windows by Petzold
Charles Petzold
Charles Petzold is an American programmer and technical author on Microsoft Windows applications. He is also a Microsoft Most Valuable Professional....

, and Windows 3.0 Power Programming Techniques by Norton
Peter Norton
Peter Norton is an American programmer, software publisher, author, and philanthropist. He is best known for the computer programs and books that bear his name. Norton sold his PC-Software business to Symantec Corporation in 1990....

 & Yao. Symbian Foundation continues to advocate this as the recommended bracing style for Symbian OS C++ mobile phone applications.

This style puts the brace associated with a control statement on the next line, indented. Statements within the braces are indented to the same level as the braces.


while (x y)
{
something;
somethingelse;
}

finalthing;


The advantages of this style are similar to those of the Allman style in that blocks are clearly set apart from control statements. However with Whitesmiths style, the block is still visually connected to its control statement instead of looking like an unrelated block of code surrounded by whitespace. Another advantage is that the alignment of the braces with the block emphasizes the fact that the entire block is conceptually (as well as programmatically) a single compound statement. Furthermore, indenting the braces emphasizes that they are subordinate to the control statement.

A disadvantage of this style could be that the braces do not stand out as well. However this is largely a matter of opinion, because the braces occupy an entire line to themselves even if they are indented to the same level as the block.

Another disadvantage could be that, if certain convenience grammar elements, such as 'else if', are employed, the ending brace no longer lines up with the statement it conceptually belongs to, although others argue that the closing brace belongs to the opening brace and not to the control statement.

An example:

if (data != NULL && res > 0)
{
if (!JS_DefineProperty(cx, o, "data", STRING_TO_JSVAL(JS_NewStringCopyN(cx, data, res)),
NULL, NULL, JSPROP_ENUMERATE))
{
QUEUE_EXCEPTION("Internal error!");
goto err;
}
PQfreemem(data);
}
else if (!JS_DefineProperty(cx, o, "data", OBJECT_TO_JSVAL(NULL),
NULL, NULL, JSPROP_ENUMERATE))
{
QUEUE_EXCEPTION("Internal error!");
goto err;
}


However, if one adopts the styling rule that braces will be provided to every level of 'scope', then the above code could be written to replace the 'else if' with a separated 'if' in the scope of a clearly roped-off 'else' portion of the statement.


if (data != NULL && res > 0)
{
if (!JS_DefineProperty(cx, o, "data", STRING_TO_JSVAL(JS_NewStringCopyN(cx, data, res)),
NULL, NULL, JSPROP_ENUMERATE))
{
QUEUE_EXCEPTION("Internal error!");
goto err;
}
PQfreemem(data);
}
else
{
if (!JS_DefineProperty(cx, o, "data", OBJECT_TO_JSVAL(NULL),
NULL, NULL, JSPROP_ENUMERATE))
{
QUEUE_EXCEPTION("Internal error!");
goto err;
}
}


Following the strategy shown above, some would argue the code is inherently more readable, however issues arise in readability as more conditions are added, shown in this pseudo-code (although usually in this case, a switch statement would suffice)

else
{
if (stuff is true)
{
Do stuff
}
else
{
if (other stuff is true)
{
Do other stuff
}
else
{
if (its still not true)
{
Do even more other stuff
}
}
}
}

GNU style
Like the Allman and Whitesmiths styles, GNU
GNU
GNU is a Unix-like computer operating system developed by the GNU project, ultimately aiming to be a "complete Unix-compatible software system"...

 style puts braces on a line by themselves, indented by 2 spaces, except when opening a function definition, where they are not indented. In either case, the contained code is indented by 2 spaces from the braces.

Popularised by Richard Stallman
Richard Stallman
Richard Matthew Stallman , often shortened to rms,"'Richard Stallman' is just my mundane name; you can call me 'rms'"|last= Stallman|first= Richard|date= N.D.|work=Richard Stallman's homepage...

, the layout may be influenced by his background of writing Lisp
Lisp programming language
Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally specified in 1958, Lisp is the second-oldest high-level programming language in widespread use today; only Fortran is older...

 code. In Lisp the equivalent to a block (a progn) is a first class data entity and giving it its own indent level helps to emphasize that, whereas in C a block is just syntax. Although not directly related to indentation, GNU coding style also includes a space before the bracketed list of arguments to a function.


static char *
concat (char *s1, char *s2)
{
while (x y)
{
something ;
somethingelse ;
}
finalthing ;
}


This style combines the advantages of Allman and Whitesmiths, thereby removing the possible Whitesmiths disadvantage of braces not standing out from the block.

The GNU Emacs text editor and the GNU systems' indent
Indent (Unix)
indent is a Unix utility that reformats C and C++ code in a user-defined indent style and coding style. Support for C++ code is considered experimental.-Examples of usage:The command line...

 command will reformat code according to this style by default. It is mandated by the GNU Coding Standards
GNU Coding Standards
The GNU Coding Standards are a set of rules and guidelines for writing programs that work consistently within the GNU system. The GNU Coding Standards were written by Richard Stallman and other GNU Project volunteers. The standards document is part of the GNU Project and is available from the GNU...

 and is used by nearly all maintainers of GNU project
GNU Project
The GNU Project is a free software, mass collaboration project, announced on September 27, 1983, by Richard Stallman at MIT. It initiated GNU operating system development in January, 1984...

 software, but is rarely used outside the GNU
GNU
GNU is a Unix-like computer operating system developed by the GNU project, ultimately aiming to be a "complete Unix-compatible software system"...

 community. Another disadvantage is that the ending brace no longer lines up with the statement it conceptually belongs to.

Those who do not use GNU Emacs, or similarly extensible/customisable editors, may find that the automatic indenting settings of their editor are unhelpful for this style. However, many editors defaulting to KNF style cope well with the GNU style when the tab width is set to 2 spaces; likewise, GNU Emacs adapts well to KNF style just by setting the tab width to 8 spaces. In both cases, automatic reformatting will destroy the original spacing, but automatic line indentation will work correctly.

Horstmann style

The 1997 edition of Computing Concepts with C++ Essentials by Cay S. Horstmann adapts Allman by placing the first statement of a block on the same line as the opening brace.


while (x y)
{ something;
somethingelse;
//...
if (x < 0)
{ printf("Negative");
negative(x);
}
else
{ printf("Non-negative");
nonnegative(x);
}
}
finalthing;


This style combines the advantages of Allman by keeping the vertical alignment of the braces for readability and easy identification of blocks, with the saving of a line of the K&R style. However the 2003 edition now uses Allman style throughout. http://www.horstmann.com/bigcpp/styleguide.html
Pico style
The style used most commonly in the Pico programming language
Pico programming language
Pico is a programming language developed at the Software Languages Lab at Vrije Universiteit Brussel. The language was created to introduce the essentials of programming to non-computer science students....

 by its designers is different from the aforementioned styles. The lack of return statements and the fact that semicolons are used in Pico as statement separators, instead of terminators, leads to the following syntax:

stuff(n):
{ x: 3 * n;
y: doStuff(x);
y + x }


The advantages and disadvantages are similar to those of saving screen real estate with K&R style. One additional advantage is that the beginning and closing braces are consistent in application (both share space with a line of code), as opposed to K&R style where one brace shares space with a line of code and one brace has a line to itself.
Banner style
The banner style can make visual scanning easier for some, since the "headers" of any block are the only thing extented at that level (the theory being that the closing control of the previous block interferes with the header of the next block in the K&R and Allman styles). In this style, which is to Whitesmiths as K&R is to Allman, the closing control is indented as the last item in the list (and thus appropriately loses salience).


function1 {
dostuff
do more stuff
}

function2 {
etc
}


or, in a markup language...








lots of stuff...
more stuff
alternative for short lines etc.



... etc


Lisp style
A programmer may even go as far as to insert closing brackets in the last line of a block. This style makes indentation the only way of distinguishing blocks of code, but has the advantage of containing no uninformative lines. This could easily be called the Lisp style (because this style is very common in Lisp code) or the Python style (Python has no brackets, but the layout looks very similar, as evidenced by the following two code blocks).


// In C
for (i = 0; i < 10; i++) {
if (i % 2 0) {
doSomething (i); }
else {
doSomethingElse (i); } }


 

# In Python
for i in range(10):
if i % 2

0:
doSomething(i)
else:
doSomethingElse(i)


 

;; In Lisp
(dotimes (i 10)
(if (evenp i)
(do-something i)
(do-something-else i)))

Compact Control Readability style
This style makes it easy to skim the left edge of the code for control statements (whereas styles like 1TBS make statements such as "else" harder to see because they are after an end bracket on the same line). However it keeps the code more compact than styles like the Allman style, by putting opening brackets at the end of lines (as opposed to on their own lines).


// In JS
if (x y) {
doSomethingA;
doSomethingB;
}
else {
doSomethingC;
doSomethingD;
}

Losing track of blocks

In certain situations, there is a risk of losing track of block boundaries. This is often seen in large sections of code containing many compound statements nested to many levels of indentation - by the time the programmer scrolls to the bottom of a huge set of nested statements, he may have lost track of which control statements go where.

Programmers who rely on counting the opening braces may have difficulty with indentation styles such as K&R, where the beginning brace is not visually separated from its control statement
Control flow
In computer science, control flow refers to the order in which the individual statements, instructions, or function calls of an imperative or a declarative program are executed or evaluated....

. Programmers who rely more on indentation 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 for
For loop
In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement....

, one can use a large indent, 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, as well as using the K&R style.

In Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

- or Linux
Linux
Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component of any Linux system is the Linux kernel, an operating system kernel first released October 5, 1991 by Linus Torvalds...

-style systems, where vi
Vi
vi is a screen-oriented text editor originally created for the Unix operating system. The portable subset of the behavior of vi and programs based on it, and the ex editor language supported within these programs, is described by the Single Unix Specification and POSIX.The original code for vi...

 or vim are often the default text editor
Text editor
A text editor is a type of program used for editing plain text files.Text editors are often provided with operating systems or software development packages, and can be used to change configuration files and programming language source code....

s, one method for tracking block boundaries is to position the text cursor over one of the braces, and pressing the '%' key. Vi or vim will then bounce the cursor to the opposing brace. Since the text cursor's 'next' key (viz., the 'n' key) retained directional positioning information (whether the 'up' or 'down' key was previously pressed), the dot macro (the '.' key) could then be used to place the text cursor on the next brace, given an appropriate coding style. Alternatively, inspection of the block boundaries using the '%' key can be used to enforce a coding standard.

Another way is to use inline comments added after the closing brace:


for (int i = 0; i < total; i++) {
foo(bar);
} //for (i)


 

if (x < 0) {
bar(foo);
} //if (x < 0)


However, maintaining duplicate code in multiple locations is the major disadvantage of this method.

Another solution is implemented in a folding editor
Folding editor
A folding editor is a text editor which supports text folding or code folding, a mechanism allowing the user to hide and reveal blocks of text—usually named...

, which lets the developer hide or reveal blocks of code by their indentation level or by their compound statement structure. Many editors will also highlight matching brackets or braces when the caret is positioned next to one.

Statement insertion

K&R style prevents another common error suffered when using the standard UNIX
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 line editor, ed
Ed (text editor)
ed is a line editor for the Unix operating system. It was one of the first end-user programs hosted on the system and has been standard in Unix-based systems ever since. ed was originally written in PDP-11/20 assembler by Ken Thompson in 1971...

. A statement mistakenly inserted between the control statement and the opening brace of the loop block turns the body of the loop into a single trip.


for (int i = 0; i < total; i++)
whoops(bar); /* repeated total times, with i from 0 to (total-1) */
{
only_once; /* Programmer intended this to be done total times */
} //for (i) <-- This comment is no longer valid, and is very misleading!


K&R style avoids this problem by keeping the control statement and the opening brace on the same line.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK