Mandatory use of braces

前端 未结 21 2302
旧时难觅i
旧时难觅i 2021-02-12 23:26

As part of a code standards document I wrote awhile back, I enforce \"you must always use braces for loops and/or conditional code blocks, even (especially) if they\'re only one

相关标签:
21条回答
  • 2021-02-13 00:01

    I am not saying it is unreasonable, but in 15+ years of coding with C-like languages, I have not had a single problem with omitting the braces. Commenting out a branch sounds like a real problem in theory - I've just never seen it happening in practice.

    0 讨论(0)
  • 2021-02-13 00:02

    I enforce this to a point, with minor exceptions for if statements which evaluate to either return or to continue a loop.

    So, this is correct by my standard:

    if(true) continue;
    

    As is this

    if(true) return;
    

    But the rule is that it is either a return or continue, and it is all on the same line. Otherwise, braces for everything.

    The reasoning is both for the sake of having a standard way of doing it, and to avoid the commenting problem you mentioned.

    0 讨论(0)
  • 2021-02-13 00:04

    Many languanges have a syntax for one liners like this (I'm thinking of perl in particular) to deal with such "ugliness". So something like:

    if (foo)     
    //bar
    else     
    //baz
    

    can be written as a ternary using the ternary operator:

    foo ? bar : baz
    

    and

    while (something is true)
    {
    blah 
    }
    

    can be written as:

    blah while(something is true)
    

    However in languages that don't have this "sugar" I would definitely include the braces. Like you said it prevents needless bugs from creeping in and makes the intention of the programmer clearer.

    0 讨论(0)
  • 2021-02-13 00:04

    I have yet to have anyone come up with a good reason not to always use curly braces.

    The benefits far exceed any "it feels ugly" reason I've heard.

    Coding standard exist to make code easier to read and reduce errors.

    This is one standard that truly pays off.

    0 讨论(0)
  • 2021-02-13 00:05

    For things like this, I would recommend just coming up with a configuration template for your IDE's autoformatter. Then, whenever your users hit alt-shift-F (or whatever the keystroke is in your IDE of choice), the braces will be automatically added. Then just say to everyone: "go ahead and change your font coloring, PMD settings or whatever. Please don't change the indenting or auto-brace rules, though."

    This takes advantage of the tools available to us to avoid arguing about something that really isn't worth the oxygen that's normally spent on it.

    0 讨论(0)
  • 2021-02-13 00:06

    My personal rule is if it's a very short 'if', then put it all on one line:

    if(!something) doSomethingElse();
    

    Generally I use this only when there are a bunch of ifs like this in succession.

    if(something == a) doSomething(a);
    if(something == b) doSomething(b);
    if(something == b) doSomething(c);
    

    That situation doesn't arise very often though, so otherwise, I always use braces.

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