What’s the importance of indenting the code?

Asked

Viewed 8,945 times

8

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 indented code? It would be only legibility merit?

Besides the programmer, whoever reads the code is the compiler. It makes some difference to him, in languages that do not require indentation, processing the code with a good/bad/no indentation?

  • 2

    22h of the night , you tired , deployment turn or something , ai need to analyze a code of a language that does not dominate much in a module of the system that has a poor knowledge , do not need to change only take a doubt , then you get a messy, unidentified code ...

  • Note: Python indentation is required, yes.

  • Yeah, but it’s indentation or indentation? :)

  • 1

    @Murillogoulart, the correct is indentation. Identar does not exist in Portuguese.

  • @Andersoncarloswoss good comment, an additional: we have a community about Portuguese on the network and has this question including on the subject Ident, indent, or demonize?

4 answers

12


Serves to keep code easier to understand.

For example, look at 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 see the same code with a very badly indented:

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 well-indented code, it’s much easier to see straight away how the structures are nested. Already in the code without indentation, what the human eye and brain see is just a soup of open and close keys, ifs, whiles, etc and it gets hard to know where things start and where they end. In the poorly indented code, it’s even worse, because there’s an extra mental effort in realizing that the suggested indentation is inadequate and understanding which would be the correct one.

Anyway, it all comes down to making it easier for a human to read the code, not 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, the z() seems to be inside the if, but it’s out. In the second case, the else is in the if internal, but was indented as if it were in the external. In the third case, as the line of the y() was commented, the z() ended up hitchhiking inside the if. These pathological cases are avoided if you always delimit the scope with keys (or the equivalent depending on the language) or use an indentation-sensitive language like Python.

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

2

To reply from @Victor deals perfectly with languages Curly bracketed, others focus on more conceptual issues

In Python, bad code is not even valid code:

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

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

Also note that identation defines the blocks. So, if it happens suricate, then we execute seboso, if it doesn’t happen, we don’t execute seboso; already executoso is always executed, independent of the return of suricate.

The issue of indentation and blocks of code in Python is so strong that statements which require a code block then need to have at least one operation of noop identada (see this question: What is 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 don’t know any other language that values strong identation as Python values, normally identation is something the author does for the code reader, being stuck to syntactic elements.

2

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

Indentation is completely irrelevant to the compiler, as it eliminates any characters not significant to its analysis.

But remember:

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

More about the topic can be found here.

1

The indentation is very important. For people who follow programming professions, we must bear in mind that we are not working alone and that other people may have to take our work. Who 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.