Alex Rivera | Logout

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

Asked 2009-03-27T21:16:28.900
18
if (condition) { /* do something */ }
else { /* do something */ }

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

I was told that the first instance wasn't a good idea. I have no idea whether this is really this case (or for the second one either); does it not shorten the amount to type? Or is it because it just makes a mess?

Edit
Report

1 Answer

0

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
}
answered 2009-03-27T21:33:56.567

Your Answer