why are assignments in conditions bad?

2019-01-25 02:17发布

I am using NetBeans for PHP 6.5.

In my code I frequently use the following type of command:

if (($row = $db->get_row($sql))) {
        return $row->folder;
    } else {
        return FALSE;
    }

Netbeans tells me that I should not be using assignments in the IF statement.

Why ?

6条回答
爷、活的狠高调
2楼-- · 2019-01-25 02:43

It's probably trying to help you avoid the dreaded typo:

if(a = b)
   //logic error

Although I would expect an enviroment smart enough to warn you about that, to also be smart enough to have "oh, don't worry about that case" conditions.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-25 02:44

In languages that allways return a value on assignments it's not bad (I think it's quite common in functional languages), but (as others allready have said while I typed this) it should usually be avoided since you or someone else might mistake it for a comparison. The compiler should usually warn about it, but it can be ignored if you're sure what you're doing...

查看更多
疯言疯语
4楼-- · 2019-01-25 02:54

They are not bad, but they can lead to dangerous mistakes.

In c like languages, where an assignment is an expression, (to support for example a=b=c=1;) a common error is:

if (a = 1) { .. }

But you wanted to have

if (a == 1) { .. }

Some developers have learned to type

if (1 == a) { .. }

To create an error if one '=' is forgotten. But I think that it does not improve the readability.

However modern compilers, give a warning if you write

if (a = 1) { .. }

which I think is a better solution. In that case you are forced to check if it was what you really meant.

查看更多
姐就是有狂的资本
5楼-- · 2019-01-25 03:06

how would a code look like if you do not assign the $row value in the loop condition this would be much more complicated i think... although not that good to read for some maintainers, no? well you can do it like

$next = mysql_fetch_assoc($result)
do{
...
...
...

$next = mysql_fetch_assoc($result) or break;
}while ($next)
查看更多
一纸荒年 Trace。
6楼-- · 2019-01-25 03:08

Conditionals often include short circuit operators. So, given this example:

if ( a=func(x) && b=func(y) )
{
  // do this
}

It may not be immediately obvious, but the second assignment would only occur if the first returned >0, and if func(y) had other side effects that you were expecting, they would not happen either.

In short, if you know what you are doing and understand the side effects, then there is nothing wrong with it. However, you must consider the possibility that someone else may be maintaining your code when you're gone and they might not be as experienced as you.

Also, future maintainers may think you intended the following:

if ( a==func(x) && b==func(y) ) ...

If they "fix" your code, they actually break it.

查看更多
来,给爷笑一个
7楼-- · 2019-01-25 03:08

I use them all the time, with loops (not sure why that would make a difference), like:

$counter = 0;
while( $getWhateverDataObj = mysql_fetch_object( $sqlResult )) {
   $getWhateverObj->firstName[$counter] = $getWhateverDataObj->firstName;
   $getWhateverObj->lastName[$counter]  = $getWhateverDataObj->lastName;
   $counter++;
}

And it works fine.

查看更多
登录 后发表回答