What to put in the IF block and what to put in the

2019-02-08 16:22发布

This is a minor style question, but every bit of readability you add to your code counts.

So if you've got:

if (condition) then
{
   // do stuff
}
else
{
   // do other stuff
}

How do you decide if it's better like that, or like this:

   if (!condition) then
   {
     // do other stuff
   {
   else
   {
     // do stuff
   }

My heuristics are:

  1. Keep the condition positive (less mental calculation when reading it)
  2. Put the most common path into the first block

23条回答
姐就是有狂的资本
2楼-- · 2019-02-08 16:33

I always keep the most likely first.

In Perl I have an extra control structure to help with that. The inverse of if.

unless (alive) {
     go_to_heaven;
} else {
     say "MEDIC";
}
查看更多
迷人小祖宗
3楼-- · 2019-02-08 16:34

I think that for a single variable the not operator is simple enough and naming issues start being more relevant.

Never name a variable not_X, if in need use a thesaurus and find an opposite. I've seen plenty of awful code like

if (not_dead) {
} else {
}

instead of the obvious

if (alive) {
} else {
}

Then you can sanely use (very readable, no need to invert the code blocks)

if (!alive) {
} else {
}

If we're talking about more variables I think the best rule is to simplify the condition. After a while projects tend to get conditions like:

if (dead || (!dead && sleeping)) {
} else {
}

Which translates to

if (dead || sleeping) {
} else {
}

Always pay attention to what conditions look like and how to simplify them.

查看更多
Luminary・发光体
4楼-- · 2019-02-08 16:35

If you have both true and false conditions then I'd opt for a positive conditional - This reduces confusion and in general I believe makes your code easier to read.

On the other hand, if you're using a language such as Perl, and particularly if your false condition is either an error condition or the most common condition, you can use the 'unless' structure, which executes the code block unless the condition is true (i.e. the opposite of if):

unless ($foo) {
    $bar;
}
查看更多
爷、活的狠高调
5楼-- · 2019-02-08 16:35

You should always put the most likely case first. Besides being more readable, it is faster. This also applies to switch statements.

查看更多
走好不送
6楼-- · 2019-02-08 16:37

Software is knowledge capture. You're encoding someone's knowledge of how to do something.

The software should fit what's "natural" for the problem. When in doubt, ask someone else and see what people actually say and do.

What about the situation where the "common" case is do nothing? What then

if( common ) {
    // pass
}
else {
    // great big block of exception-handling folderol
}

Or do you do this?

if( ! common ) {
    // great big block of except-handling folderol
}

The "always positive" rule isn't really what you want first. You want to look at rules more like the following.

  1. Always natural -- it should read like English (or whatever the common language in your organization is.)
  2. Where possible, common cases first -- so they appear common.
  3. Where possible use positive logic; negative logic can be used where it's commonly said that way or where the common case is a do-nothing.
查看更多
我只想做你的唯一
7楼-- · 2019-02-08 16:38

You can usually make the condition positive without switching around the if / else blocks.

Change

   if (!widget.enabled()) {
     // more common 
   } else {
     // less common 
   }

to

   if (widget.disabled()) {
     // more common 
   } else {
     // less common
   }
查看更多
登录 后发表回答