What is "One Level of indentation"?

Asked

Viewed 247 times

9

I was watching this the other day video lesson on Laracasts, where there is an explanation that teaches to avoid accumulating too much code in one method, separating into several methods with specific responsibilities and thus reducing the size of the indentation - at least that’s what I understood, correct me if I’m wrong.

I also realized that there is the same "teaching" at that link, which I think is the Microsoft blog.

What is curious is that in these videos lessons of Laracast, usually the subjects dealt with are the framework in PHP called Laravel. I don’t know if it’s just a coincidence, but I saw that the link above is from Microsoft, and the creator of Laravel, Taylor Otwell, said in a video class that, before programming in PHP, he was programming in ASP - which is from Microsoft.

This last statement may seem irrelevant, but it is important to my question:
This is a specific pattern to ASP development (or anything else from Microsoft, like C# and so on), or actually this "One Level of Indentation" is a design standard or an encoding standard?

Note: I don’t know much about the "things" of Microsoft, so forgive me if I said something silly.

  • 1

    Object calisthenics, falls into the category of good practice or if you preface recommendations here has some others, this serves for any language.

  • Great @rray. Precious information :D

  • You could have that in English, huh :D

  • 1

    Repositories and Service Layers help with this. Instead of the Controller having direct contact with the Model, these repositories and service layers do all the work. What helps in a little code in Controller, in Identation and prevents the accumulation of repeated codes.

  • @rray after your reference, I could "learn" how to research on the subject. I think it’s worth seeing this here Object Calisthenics - English

  • 7

    It’s a "good practice", I mean, something that if you follow just by following, will screw you.

  • 1

    For practice to be good, you have to have practice. There are people who leave using the design standards and "good practices" just because they find it cute, without understanding the meaning :D

  • 2

    I don’t know if the microsoft and Laravel tags fit this question ...

  • 4

    How I lost the timing I’m not going to answer the question, but this is one of those crazy rules that someone makes up and a lot of fools follow. There is no advantage in forcing this. If something should only have a level of indent It must be because it makes sense there, not because some silly rule dictates it. Just as it is bad to put several responsibilities together in a single method, separating responsibilities that are closely related can also be bad. Complication without gain.

Show 4 more comments

3 answers

8

This type of recommendation is independent of language. Don’t get too attached to project or coding default settings, or if something comes from Microsoft or PHP.

Try to separate the concept from the implementation itself. You might as well apply this concept in COBOL as well, what’s the problem?

Try to analyze what languages have in common. It’s object orientation? So most likely you can apply the principles of SOLID.

Following everything they say is good practice is also not a good idea. Singleton was already considered a good idea, but in the current context is considered a Anti-pattern.

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

  • 2

    Good points, well done. My own COBOL will have only one level of recoil, but that help for understanding could easily be under by having a multi-screen "case statement" . He is not following the words he receives from good practice, but understanding the intention, not just the letter of the law.

8


They’re referring to the indentation of the code. To maintain the readability of the code they are advocating that only one indentation level 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 must eliminate the second indentation level by transporting 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 is a good practice to improve the readability of your code.

5

This is independent of language or environment. It’s just a good practice suggestion in code writing. And not to think that I just speak, I programmed in ASP (classic), before entering PHP, around 1999. The recommendation of levels of identation 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. In fact, I particularly disagree with having only one level because 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 many levels of identation, such as

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

                }
            }
        }
    }
}

Here we have four levels. Whether it’s acceptable or not depends on the context, the real reason for using it that way. However, if you can reduce it is always better.

When you realize that you are creating conditionals within conditionals, take a moment and reflect on the logic. 'Cause when you get to that "spaghetti on probation," there might be something wrong or something that might be better written.

Browser other questions tagged

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