What is "One Level of Indentation"?

9

I was just watching those video lesson at Laracasts , where there is an explanation that teaches you to avoid accumulating too much code in a method, separating it into several methods with specific responsibilities and thus reducing the size of the indentation - at least that is what I understood, correct me if I am wrong.

I also noticed that the same "teaching" exists in this link , which seems to me to be the Microsoft blog.

What is curious is that, in these videos of Laracast classes, the subjects usually deal with the PHP framework called Laravel. I do not know if it's just a coincidence, but I've seen that link above is from Microsoft, and Laravel's creator, Taylor Otwell, said in a video lesson that before programming in PHP he was programming in ASP - which is Microsoft .

This last placement may seem irrelevant, but it is important to my question:
Is this a specific standard for ASP development (or anything else from Microsoft, such as C # and etc.), or is this "One Level of Indentation" really a design pattern or a coding standard?

Note : I do not know much about Microsoft's "stuff", so please forgive me if I said anything silly.

    
asked by anonymous 21.03.2016 / 13:35

3 answers

8

They are referring to code indentation. To keep the code legible they are arguing that only one level of indentation should be used.

The following example would be out of this pattern.

public void func1(){
    for( i in list){//Primeiro nível de indentação
       if( i.foo == true ){//Segundo nível de identação
         //faça coisas
       }
    }
}

To fix it you should eliminate the second level of indentation by moving it into a function.

public void func1(){
    for( i in list){//Primeiro nível de indentação
       facaCoisaComI(i);
    }
}

public void facaCoisaComObjeto(Objeto i){
   if( i.foo == true ){
         //faça coisas
   }
}

This pattern is not specific to any technology or language. It's a good practice to improve the readability of your code.

    
21.03.2016 / 13:54
8

This type of recommendation is independent of language. Do not stick too much to design defaults or coding, or if something comes from Microsoft or PHP.

Try to separate the concept from the implementation itself. You can very well apply this concept in COBOL too, what's the problem?

Try to analyze what languages have in common. Is the orientation to objects ? So most likely you can apply the SOLID principles .

Following everything they say is good practice is also not a good idea. Singleton has already been considered a good idea, but in the current context it is considered a Anti -Pattern .

In short, try to interpret the ideas and be critical, understand why they say that such a thing is good and makes sense in the context you are applying.

    
22.03.2016 / 03:38
5

This is independent of language or environment. It is just a suggestion of good practice in writing codes. And to not think that I only speak by speaking, I programmed in ASP (classic), before entering PHP, around 1999. The recommendation of levels of indentation is also a matter of personal choice. There is no rule or even a widely accepted standard that says it should be so with only one level. Incidentally, I particularly disagree with having only one level because I imagine the situation as in the example posted by Vinicius Zaramella,

for( i in list){//Primeiro nível de indentação
   if( i.foo == true ){//Segundo nível de identação
     //imagine que aqui precise fazer apenas 1 ou 2 linhas de código
     //particularmente acho desnecessário ter o trabalho de criar um novo método para algo tão pequeno.
   }
}

Regardless, always avoid too many levels of indentation, such as

if () {
    if () {
        if () {
            if () {
                if () {

                }
            }
        }
    }
}

Here we have 4 levels. Whether it's acceptable or not depends on the context, the real reason to use it that way. However, if you can reduce it is always better.

When you realize that you are creating conditionals within conditionals, you should stop and reflect on logic. For when it arrives in this "spaghetti of conditionals," there may be something wrong or something that can be better written.

    
22.03.2016 / 05:40