What's the purpose of using braces (i.e. {}) for a single-line if or loop?

前端 未结 23 1229
独厮守ぢ
独厮守ぢ 2020-11-28 00:33

I\'m reading some lecture notes of my C++ lecturer and he wrote the following:

  1. Use Indentation // OK
  2. Never rely on operator preced
相关标签:
23条回答
  • 2020-11-28 01:16

    One option for helping to prevent the errors that have been described above is to inline what you want to happen when you don't use braces. It makes it much harder to not notice the errors when you try to modify the code.

    if (condition) doSomething();
    else doSomethingElse();
    
    if (condition) doSomething();
        doSomething2(); // Looks pretty obviously wrong
    else // doSomethingElse(); also looks pretty obviously wrong
    
    0 讨论(0)
  • 2020-11-28 01:18

    The most pertinent example I can think of:

    if(someCondition)
       if(someOtherCondition)
          DoSomething();
    else
       DoSomethingElse();
    

    Which if will the else be paired with? Indentation implies that the outer if gets the else, but that's not actually how the compiler will see it; the inner if will get the else, and the outer if doesn't. You would have to know that (or see it behave that way in debugging mode) to figure out by inspection why this code might be failing your expectations. It gets more confusing if you know Python; in that case you know that indentation defines code blocks, so you would expect it to evaluate according to the indentation. C#, however, doesn't give a flying flip about whitespace.

    Now, that said, I don't particularly agree with this "always use brackets" rule on its face. It makes code very vertically noisy, reducing the ability to read through it quickly. If the statement is:

    if(someCondition)
       DoSomething();
    

    ... then it should be written just like this. The statement "always use brackets" sounds like "always surround mathematical operations with parentheses". That would turn the very simple statement a * b + c / d into ((a * b) + (c / d)), introducing the possibility of missing a close-paren (the bane of many a coder), and for what? The order of operations is well-known and well-enforced, so the parentheses are redundant. You'd only use parentheses to enforce a different order of operations than would normally be applied: a * (b+c) / d for instance. Block braces are similar; use them to define what you want to do in cases where it differs from the default, and is not "obvious" (subjective, but usually pretty common-sense).

    0 讨论(0)
  • 2020-11-28 01:20

    it is more intuitive and easily understandable. It makes the intent clear.

    And it ensures that the code doesn't break when a new user might unknowingly miss the {, } while adding a new code statement.

    0 讨论(0)
  • 2020-11-28 01:21

    I consider the first one to be clear then second. It gives the feeling of closing instructions, with little code is fine when code gets complex {...} helps a lot even if it is endif or begin...end

    //first
    int j = 0;
    for (int i = 0 ; i < 100 ; ++i)
    {
        if (i % 2 == 0)
        {
            j++;
        }
    }
    
    
    //second
    int j = 0;
    for (int i = 0 ; i < 100 ; ++i)
        if (i % 2 == 0)
            j++;
    i++;
    
    0 讨论(0)
  • 2020-11-28 01:22

    It's very easy to accidentally change control-flow with comments if you do not use { and }. For example:

    if (condition)
      do_something();
    else
      do_something_else();
    
    must_always_do_this();
    

    If you comment out do_something_else() with a single line comment, you'll end up with this:

    if (condition)
      do_something();
    else
      //do_something_else();
    
    must_always_do_this();
    

    It compiles, but must_always_do_this() isn't always called.

    We had this issue in our code base, where someone had gone in to disable some functionality very quickly before release. Fortunately we caught it in code review.

    0 讨论(0)
  • 2020-11-28 01:22

    The codebase I'm working on is scattered with code by people with a pathological aversion to braces, and for the people who come along later, it really can make a difference to maintainability.

    The most frequent problematic example I have encountered is this:

    if ( really incredibly stupidly massively long statement that exceeds the width of the editor) do_foo;
        this_looks_like_a_then-statement_but_isn't;
    

    So when I come along and wish to add a then-statement, I can easily end up with this if I'm not careful:

    if ( really incredibly stupidly massively long statement that exceeds the width of the editor) do_foo;
    {
        this_looks_like_a_then-statement_but_isn't;
        i_want_this_to_be_a_then-statement_but_it's_not;
    }
    

    Given that it takes ~1second to add braces and can save you at minimum a few confused minutes debugging, why would you ever not go with the reduced-ambiguity option? Seems like false economy to me.

    0 讨论(0)
提交回复
热议问题