Are singleline if statements or if statements with

2019-02-08 02:05发布

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?

12条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-08 02:46

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
}
查看更多
再贱就再见
3楼-- · 2019-02-08 02:47

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 ", "
}
查看更多
手持菜刀,她持情操
4楼-- · 2019-02-08 02:54

The best practice is to write code that others can read and update easily.

Your first form is questionable because it doesn't follow the forms that most PHP developers are used to:

if (condition) {
  // code
} else {
  // code
}

// ... or ...

if (condition)
{
  // code
}
else
{
  // code
}

// ... or ...

if (condition) { /* short code */ } else { /* short code */ }

// ... or ...

condition ? /* short code */ : /* short code */;

Note that this is entirely about standard practice, and doesn't necessarily make sense—it's only about what other developers are used to seeing.

Your second form, more importantly, isn't so good because it makes it easy for another programmer to make this mistake:

if (condition)
  // code A
else
  // code B
  // code C (added by another programmer)

In this example, the other programmer added code C, but forgot to wrap the whole else block in braces. This will cause problems. You can defend against this by simply wrapping your if and else blocks in braces.

查看更多
\"骚年 ilove
5楼-- · 2019-02-08 02:55

This is something that I actually remember from an employment exam a while back. The code was similar to the following:

if (x == 0)
    x = 2;
else
    print("x is: %d", x); // debugging!
    x = 4;

Most people here can spot the error, but you can really substitute in anything you want as the "bad code" that was inserted. The more subtle error comes when you have an "old version" of something commented out, and somebody un-comments it, and suddenly the second statement is outside the block.

Basically, unless it's a small test application to learn a concept fast, I always bracket (and even in the test apps I usually bracket). It just isn't worth the headache later if I don't, even in 5-line-methods.

查看更多
Viruses.
6楼-- · 2019-02-08 03:04

This is more coding style than anything else. That said, my personal opinion is that your second example is potentially quite harmful. It's easy enough to accidentally "add a second line to the block" in languages where braces are the only way to create blocks. But in PHP, where an alternate syntax exists, this is even less likely to set off the necessary warning bells:

if ($_GET["asdf"]==1):
    /* do something */
else:
    /* do something */
endif;

Rule of thumb: if you're going to put your "do something" on a separate line, use braces; if you're not going to use braces, put it on the same line!

查看更多
冷血范
7楼-- · 2019-02-08 03:05

The problem I've seen is developers not recognizing the {}-less-if when they add code to one of the conditions. Example:

//before
if(something)
    statement;

//after
if(something)
    statement;
    addedstatement;

Obviously, this won't do what they expect.

查看更多
登录 后发表回答