views:

407

answers:

10

Sorry if this is a comp-sci 101 question. I'm just unsure if I'm missing something obvious.

So let's say some user input throws an error, and I want to catch it and return some feedback. The error will be a number, 0 - 8. 0 means "No Error". I want to give the user very specific feedback if the error is 3 (No numbers allowed, let's say). For the other 7 possibilities, I just want to end the script and echo the error.

I was working this out and decided to go with this:

$no_errors ($_error != 0 || $_error != 3) ? FALSE : TRUE;
if (!$no_errors)
echo $error_msg['$_error'];
$error_3 ($no_errors && $_error == 3) ? TRUE : FALSE;
if ($error_3)
   bunch of stuff happens;
else
   bunch of other stuff;

Anyways, I was then noticing the OR operator on the first line and was thinking that it might be better/safer to user an AND operator. But the more I contemplate, the less I see a difference.

So the real question is, if you want to eliminate two possibilities of a specific variable, are AND and OR identical, or is one logically/functionally more optimal?

+2  A: 

Performance-wise, keep in mind that that the evaluation will be lazy in most languages. Using OR, if the first condition is true, it will return true without evaluating the second condition. For AND, it will return false if the first conditions is false, without evaluating the second.

Otherwise, the performance of the operators themselves is not really different. Use what is most readable to you.

wvanbergen
A: 

(A || B) = !(A && B). So it really doesn't make a difference

EDIT: (A || B) = !(A && B) is wrong. Tks for the comment. The correct form is (!A || !B) = !(A && B).

My apologies

Megacan
Michał Rudnicki
Well that's totally true, my bad : (.
Megacan
+1  A: 

The best is the one that helps you read code faster. It is the only true optimization you can make here, and possibly in millions of other places.

Michał Rudnicki
+1  A: 

This would look more simple to read:

  if($_error==3)
      do stuff;
    else if($_error>0)
      do some other stuff
    else
      do normal stuff

Nobody notices the microseconds that you may win.

Riho
+3  A: 

First of all, I think I would recommend using another way of identifying errors than using "magic numbers". They are hard to maintain, as you easily forget what "3" meant. It looks like your language is PHP, which has support for exceptions. I'd recommend using them instead, you can read more about them here: http://dk.php.net/exceptions

As for logical operators, there aren't really any that are considered "good practice". Use what you want. If you have trouble figuring out when your expression is true/false, try making a truth table: http://en.wikipedia.org/wiki/Truth_table

Ulrik Rasmussen
+19  A: 

It will be much more readable if you use a switch statement:

switch ($_error) {
    case 0;
        nothing happens;
        break;
    case 3:
        bunch of stuff happens;
        break;
    default:
        bunch of other stuff;
        break;
}
gkrogers
Much easier to annotate with comments as well
WestDiscGolf
+1  A: 

logically the following are identical ( excuse my pseudo code )

(! expression_one || ! expression_two) /** this is the same as the one below **/
! (expression_one && expression_two)

Functionally which one is more optimal? They are both as optimal as each other. Both ways (&& and ||) allow short circuiting if the first expression is true (in the || case) or false ( in the && case)

hhafez
A: 

Performance wise I believe they are very similar (see post by wvanbergen), however there are obvious differences in behaviour. I think that your current posting may not be doing what you are hoping for. In the first line if you $_error = 3 then you will have $no_errors = false. As you are checking for two conditions that both need to be satisfied maybe an and would be more appropriate.

Sometimes I find the easiest way to check my logic is to actually say what I want out loud. For example, "We have no errors if the error code is not 0 and not 3"

I generally find that in situation like this with only a few variables, the way I would write the sentence in English provides me with the most intuitive (and accurate) method for determining the logical operators.

If you find that the code looks messy after this process then you may find this link on De Morgan's laws useful

chillysapien
A: 

If you use:

$no_errors ($_error != 0 && $_error != 3) ? FALSE : TRUE;

This means if $error != 0 AND $error != 3 which is invalid logic because $error cannot be == to 3 and 0 it can be 3 OR 0 but not both.
In the above example if $error = 0 then it would evaluate to FALSE because it is not 3 && 0 you are looking for it to be either 3 || 0.

Unkwntech
+1  A: 
  1. Personally I would eliminate the use of those numbers and use constants instead. Besides being easier to maintain, they make the coding itself much easier and allows you to update the values tomorrow e.g. if some circumstance foreces you to change the number from 3, you have to pretty much look through all your code
  2. As suggested by gkrogers, a switch while accomplishing the same thing is much easier to read and maintain
Conrad