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

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

    My preference if for consistency... so:

    if(...)
    {
       statement 1;
       statement 2;
    }
    else
    {
       statement 1;
       statement 2;
    }
    

    is no different than:

    if(...)
    {
       statement 1;
    }
    else
    {
       statement 1;
    }
    

    So I always use them because it is consistent and it avoids problems forgetting to add them in later.

    However other people will look at my code and think that it is stupid to put in the { and }. They have their reasons, I have mine... I happen to like my reasons more than I like theirs :-)

提交回复
热议问题