Mandatory use of braces

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

    I stand on the ground that braces should match according to indentation.

    // This is right.
    if (foo) 
    {
        // bar
    }
    else 
    {
        // baz
    }
    
    while (things) 
    {
        // stuff
    }
    

    As far as your two examples, I'd consider yours slightly less readable since finding the matching closing parentheses can be hard, but more readable in cases where indentation is incorrect, while allowing logic to be inserted easier. It's not a huge difference.

    Even if indentation is incorrect, the if statement will execute the next command, regardless of whether it's on the next line or not. The only reason for not putting both commands on the same line is for debugger support.

提交回复
热议问题