Are singleline if statements or if statements without braces bad practice?

前端 未结 12 2192
天涯浪人
天涯浪人 2021-01-01 17:10
if (condition) { /* do something */ }
else { /* do something */ }

if (condition)
    /* do something */
else
    /* do something */

I was told tha

12条回答
  •  梦毁少年i
    2021-01-01 17:42

    Have you ever seen code like this in C or C++?

        /*  Warning:  bogus C code!  */
    
    if (some condition)
            if (another condition)
                    do_something(fancy);
    else
            this_sucks(badluck);
    

    Either the indentation is wrong, or the program is buggy, because an "else" always applies to the nearest "if", unless you use braces.

    (Let's just use python. No brackets, just pure clean whitespaces. :P)

提交回复
热议问题