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.


Know the details of Indentation Styles


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.


Existing patterns

Below we will see standards established by some organizations that develop software:


Style BSD

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.

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


Style K&R

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.

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

This pattern can be found, for example, in Kernel Linux.


Style GNU

GNU is an organization that develops Open Source software with licenses that allow you to change the code and redistribute(Free Software) . It was started by programmer Richard Stallman.

Notation uses the beginning of the braces after the main instruction and misaligned at the beginning of it, with a difference of one space.

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

Style Whitesmiths

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 .

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

Style Haskell

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.

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

Style Pico

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.

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

Ratliff Style

Ratliff was the original programmer(C. Wayne Ratliff) behind the popular fourth generation programming languages dBase-II and -III. This notation was found in the materials of Digital Research Inc.

Reminiscent of K&R style, but with double spaces at the beginning of each line in the block.

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

Style Lisp

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.

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

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:

void myfunction()
   int x = 42; // syntax error
{
   while (x == y)
   int y = 42; // another error
   {
       something();
       somethingelse();
   }
}

I always prefer to use it like this:

void myfunction(){
   int x = 42;
   int y = 42;
   while (x == y) {
       something();
       somethingelse();
   }
}

I call this style Like a Boss! 😃


Source: Wikipedia .


programming clanguage development haskell gnu lisp


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles