Mandatory use of braces

前端 未结 21 2342
旧时难觅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: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.

提交回复
热议问题