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条回答
Summer. ? 凉城
2楼-- · 2019-02-08 02:40

You should put the "if" and the "do something" on separate lines to make your code friendlier to interactive debuggers.

If you put both the "if" and "do something" on the same line, then you can't set a breakpoint just on the "do something" line.

查看更多
男人必须洒脱
3楼-- · 2019-02-08 02:41

For all but the shortest statements, use the braces and space them accordingly. You want to do this for a few reasons:

  • It's harder to make a mistake about where something goes.

  • It's easier to read.

  • In languages with macro-expansion facilities (e.g. C, C++), failure to include braces will cause confusing logic errors when a macro containing multiple statements is expanded inside of an unbraced if-else.

查看更多
何必那么认真
4楼-- · 2019-02-08 02:43

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 :-)

查看更多
对你真心纯属浪费
5楼-- · 2019-02-08 02:44

Generally non-readable code is a bad practice. The single line is more efficient in your typing and saves line numbers but come back to it a year from now or while you're scanning for bugs and it'll make it more difficult.

In my opinion, yes it's bad practice to have single line if statements.

The computer doesn't really care (as far as I can tell) but you should always write your code like it's going to be maintained by a serial killer that knows where you live.

Readable! Easily self-discernable.

查看更多
霸刀☆藐视天下
6楼-- · 2019-02-08 02:45

Have you ever seen code like this in C or C++?

    /*  Warning:  bogus C code!  */

if (some condition)
        if (another condition)
                do_something(fancy);
else
        this_sucks(badluck);

Either the indentation is wrong, or the program is buggy, because an "else" always applies to the nearest "if", unless you use braces.

(Let's just use python. No brackets, just pure clean whitespaces. :P)

查看更多
贪生不怕死
7楼-- · 2019-02-08 02:46

One major benefit of using multiple lines is ease of debugging. If you have an if else statement all on one line and the debugger tells you that line x blew up, it's more difficult to determine which part of the statement failed. Multiple lines also makes it easier to step through your code using a debugger.

查看更多
登录 后发表回答