Curly braces in if-else blocks

半世苍凉 提交于 2020-04-07 05:37:29

问题


I'm programming in java and I happened to find myself with this:

if(nodo == null) return null;
    else if(nodo.izquierda!=null && nodo.derecha==null)
        return nodo.izquierda;
    else if(nodo.izquierda==null && nodo.derecha!=null)
        return nodo.derecha;
    else return null; // si ambos hijos son nulos o no nulos

My question is: Is it the same to write that as:

if(nodo == null) 
    return null;
else {
    if(nodo.izquierda!=null && nodo.derecha==null)
        return nodo.izquierda;
    else {
        if(nodo.izquierda==null && nodo.derecha!=null)
              return nodo.derecha;
        else return null; // si ambos hijos son nulos o no nulos
    }
 }

More than anything I'm confused by how to use the curly braces with if-else blocks. Is it necesary to use curly braces?

For example:

if (something)
     if (something else)
     ...
     else (blah blah)

Is the same that:

if (something)
{
     if (something else)
     ...
     else (blah blah)
}

回答1:


For example:

if (something)
      if (something else)
      ...
      else (blah blah)

Is the same that:

 if (something)
   {
      if (something else)
      ...
      else (blah blah)
   }

In this case, yes, they're the same, but the former is harder to read than the latter. I recommend using braces always, even for blocks of code made of a single line.




回答2:


For Java, curly braces are optional for if-else statements. As Jared stated, only the next statement will be executed when curly braces are omitted.

Generally the braces help with organization and readability of the code, so they commonly will be used.




回答3:


  • The 2 last statements execute the same; omitting the braces is legal but is not recommended.

  • If the braces are omitted for an else if, the else if always belongs to the innermost if statement.

    REFERENCE: Link to the IF ELSE behavior if braces are Omitted
  • This URL: Link to Study of Code with Braces Omitted contains the result of an analysis and states that projects with code that omits braces for "if statements" are more likely to have bugs.
  • If a developer intentionally omits the braces (which is legal), the next developer looking at the code without braces will have to mentally "parse" each else if and match the else if to the closest if statement. It is much easier to match if to else if and else when there are braces that are indent-aligned.
  • The code is easiest to read when code inside the braces in a top-level if statement is indented 4 spaces, and code in a second-level if statement is indented 8 spaces, as in:
    if (var1 == 1) {
        action1();
    }
    else if (var1 == 2) {
        action2();
    }
    else if (var1 == 3) {
    
        if (var2 == 1) {
            action31();
        }
        else if (var2 == 2) {
            action32();
        }
    }
    




  • 回答4:


    My question is: Is it the same to write that as:

    The fact that you need to ask is a very good reason to use the braces. There are always two audiences for code, a compiler and any human who may need to read and reason about the code, including your own future self.

    Often, braces will be unnecessary for the compiler, but enhance readability for the other audience.




    回答5:


    if(nodo != null)
        if(nodo.izquierda != null && nodo.derecha == null)
            return nodo.izquierda;
        else if(nodo.izquierda == null && nodo.derecha != null)
            return nodo.derecha;
    return null;
    


    来源:https://stackoverflow.com/questions/30224071/curly-braces-in-if-else-blocks

    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!