What is the importance of indenting the code?

6

It is common to hear that it is important to keep the code indented, which is not a difficult task with the help of an IDE.

Excluding languages that require indentation, such as Python, why is it important to keep the indented code? Is it just the merit of readability?

In addition to the programmer, whoever reads the code is the compiler. Does it make any difference to him, in languages that do not require indentation, to process the code with a good / bad / no indentation?

    
asked by anonymous 16.03.2017 / 03:43

4 answers

9

It serves to keep the code easier to understand.

For example, see this code without any indentation:

if (a) {
if (b) {
while (c) {
d();
}
} else if (e) {
f();
} else if (g) {
h();
}
} else if (i) {
while (j) {
k();
}
}

Now look at the same code with a very bad indentation:

if (a) {
    if (b)
  {
        while (c) {
    d();
}
  }
else
      if (e)
 {
 f();
        }
else if (g)
       {
      h();
              }
   } else
 if (i) {
    while (j) {
      k();
         }
    }

And now the well-indented code:

if (a) {
    if (b) {
        while (c) {
            d();
        }
    } else if (e) {
        f();
    } else if (g) {
        h();
    }
} else if (i) {
    while (j) {
        k();
    }
}

Note that in nicely indented code, it's much easier to notice right away how the structures are nested. In the indentation - free code, what the human eye and brain sees is just a soup that opens and closes keys, if s, while s, etc and it 's hard to know where things start and where they end. In the badly-indented code, it's even worse, as there is an extra mental effort in realizing that the suggested indentation is inadequate and figuring out which one would be the right one.

Finally, it all boils down to making it easier to read the code to a human, not to a machine.

In particular, look at these terrible examples where the wrong indentation makes the instructions appear to be in the wrong place:

if (x)
    y();
    z();
if (a)
    if (b)
        c();
else
    d();
if (x)
    // y();

z();

In the first case, z() seems to be within if , but is out. In the second case, the else is in% internal%, but was indented as if it were in the external. In the third case, since the if line was commented out, y() ended up hitching in z() . These pathological cases are avoided if you always delimit the scope with braces (or the equivalent depending on the language) or use an indentation-sensitive language such as Python.

For the compiler, indentation almost always does not matter. The main exception is obviously Python, where the compiler uses indentation to nest structures. Another exception I remember is from Scheme / Racket, where although the compiler does not need the indentation when the code is correct, in case there is a compilation error it will use the indentation to suggest where is the most likely location of the error to have occurred .

    
16.03.2017 / 04:02
2

Indentation is a key resource for the readability of a code, as it adds important information such as: which code block belongs to a function or method? It also helps you understand the hierarchy of elements in HTML and XML code. I think every programmer already expects to find some kind of indentation when they read any code.

The indentation is completely irrelevant to the compiler, as it eliminates any non-significant characters for its parsing.

But remember:

  

Any idiot can write code that a computer understands. Good   programmers write code that humans can understand. Martin   Fowler

More on the topic can be found here .

    
16.03.2017 / 04:08
1

The indentation is quite important. For people who follow programming professions, we have to take into account that we are not working alone and that other people may have the way to get on with our work. Whoever says indentation says the use of comments.

Some large companies like Google / Microsoft even have code organization rules for different projects. These rules include indentation / comments / code organization in general etc.

    
16.03.2017 / 03:56
1
  

@Victor's answer perfectly addresses curly bracketed languages , the rest focus on more conceptual issues

In Python, poorly-indented code is not even valid code:

# inválido
if marmota:
marmotante()
executoso()

# válido 
if suricate:
    seboso()
executoso()

Also notice that the indentation defines the blocks. So if we give suricate , then we execute seboso , if it does not, we do not execute seboso ; already executoso is always executed, regardless of the return of suricate .

The question of indentation and code blocks in Python is so strong that statements that require a block of code then need to have at least a% id operation (see this question: What's the difference between break, pass and continue in python? )

# exemplo copiado da resposta da pergunta acima
def soma_de_quadrados(x, y):
    pass # noop, nenhuma operação =]

I do not know any other language that values by strong indentation as Python prides, usually the indentation is something that the author does for the code reader, being attached to syntactic elements.

    
21.10.2017 / 01:44