Know the details of Indentation Styles
The indentation style is not mandatory in most programming languages, but it says a lot about the programmer origin.
In computer programming, an Indentation (indentation style) is a convention that governs the indentation of code blocks to convey program structure.
Indentation is not a requirement of most programming languages, where it is used as a secondary notation. Instead, indentation helps to better convey the structure of a program to human readers.
It is used to better visualize the link between control flow constructs, such as conditions or loops, and the code contained inside and outside them.
However, some languages (like Python and occam ) use indentation to determine structure instead of using curly braces or keywords; this is called the off-side rule, in these languages, indentation is significant (and mandatory) for the compiler or interpreter; it’s more than just a matter of style.
Below we will see standards established by some organizations that develop software:
This style is also known as Allman (Eric Allman, creator of sendmail)
Notation uses the beginning of the braces after the main instruction and aligned to the beginning of it.
K&R are the initials of the surnames of Kerningan and Ritchie, the creators of the languages AWK and C, respectively. Both wrote the best-selling programming book in history: The C Programming Language .
Notation uses the beginning of braces on the same line after the main statement.
This pattern can be found, for example, in Kernel Linux.
Notation uses the beginning of the braces after the main instruction and misaligned at the beginning of it, with a difference of one space.
Whitesmiths was the first commercial compiler for the C language. This notation was found in the first version of Windows .
Similar to GNU, but with 2 spaces instead of one .
Haskell is a statically typed and functional programming language.
The notation is a mixture of the GNU style with the semicolon also aligned with the braces.
Pico is a programming language created to introduce the fundamentals of programming to non-computer science students.
The opening brace is not on the same line, but the closing brace is after the last statement of the block and on the same line.
Reminiscent of K&R style, but with double spaces at the beginning of each line in the block.
Lisp is a programming language and also a family of programming languages that obey the standard created by the first Lisp.
Similar to Pico, however, with leading spaces before the opening brace pair.
Remembering that this is an illustrative example, Lisps have a greater use of parentheses throughout the code.
What’s your style?
My style is more K&R, only more modern! 😃 I like to merge the opening of keys too when I create functions. I prefer this style because it lessens the likelihood of typos similar to the one below, when usage is similar to the BSD style:
I always prefer to use it like this:
I call this style Like a Boss! 😃
Source: Wikipedia .