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

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

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

I was told tha

12条回答
  •  天命终不由人
    2021-01-01 17:31

    Those are two lines long, so not really a single line.

    There's nothing wrong with single line ifs when it makes the code easier to read.

    For example, something like this:

    if (last_item) print ", and " else print ", "
    

    is much better than

    if (last_iem)
    {
        print ", and "
    }
    else
    {
        print ", "
    }
    

提交回复
热议问题