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

前端 未结 12 2204
天涯浪人
天涯浪人 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:39

    I have seen so many third party code with silly issues, that I prefer to use braces all the time. That said I have never felt good on

    if(){}
    else (){}
    

    I use if(){} on the same line when it is a short instruction and it is alone. If there is an else use the long:

    if(checkSomething)
    {
       //dosomething
    }
    else
    {
       //doanotherthing
    }
    

提交回复
热议问题