brb
brb

Reputation: 118

Curly braces in if-else blocks

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)
}

Upvotes: 2

Views: 11240

Answers (5)

hakun bahun
hakun bahun

Reputation: 79

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;

Upvotes: -1

A B
A B

Reputation: 4148

  • 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();
        }
    }
    

  • Upvotes: 2

    Luiggi Mendoza
    Luiggi Mendoza

    Reputation: 85779

    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.

    Upvotes: 7

    Patricia Shanahan
    Patricia Shanahan

    Reputation: 26185

    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.

    Upvotes: 1

    DaCrota
    DaCrota

    Reputation: 21

    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.

    Upvotes: 2

    Related Questions