6

Is there a difference between coding with if else or && and || operators.

For example in if-else style I can write this code

for( var i = 0; i < 1000000000; i ++ ) {
    if( i % 2 == 0 ) {
        f1();
    } else {
        f2();
    }
}

And in && and || style I can get same result with this code

(( i % 2 == 0 ) && (test1() || true)) || test2();

I tested them in JS, they are working approx on same time, but I didnt test them on C++. Maybe it depends compiler or language.

Is there a speed difference? Or any difference at all?

Thank you

Kijewski
  • 25,517
  • 12
  • 101
  • 143
Gor
  • 2,808
  • 6
  • 25
  • 46
  • 7
    Why make code unreadable? – Ed Heal Nov 20 '15 at 15:46
  • 3
    Readability is one fairly stark difference. – Alex K. Nov 20 '15 at 15:46
  • 3
    Also, in C++, don't forget the `? :` operator. – Tripp Kinetics Nov 20 '15 at 15:47
  • I suspect that there will not be much difference in the optimized machine code output by the compiler in either case. – Tripp Kinetics Nov 20 '15 at 15:48
  • The *ternary* operator is available in most languages. – tadman Nov 20 '15 at 15:48
  • @tadman The *conditional* operator is banned in many code conventions, for good reasons. –  Nov 20 '15 at 15:49
  • I suggest a quick look at [If Statements - What is happening in an “If(..||..)” and “If(…&&…)” construct internally?](http://stackoverflow.com/q/29141368/2307070) – Thomas Ayoub Nov 20 '15 at 15:49
  • 1
    @Rhymoid What operator do you mean? I was simply pointing out the name, as searching for "?:" won't work. – tadman Nov 20 '15 at 15:50
  • @tadman Every language specification uses the name "conditional operator" for `?:`. Except PHP's specification, but hey, it's PHP, they can't even get the precedence right. –  Nov 20 '15 at 15:53
  • Worth noting. The problem with the "conditional operator" name is it's highly ambiguous as there are many other operators that operate conditionally, like most logical comparisons. [The Wikipedia page](https://en.wikipedia.org/wiki/%3F:) calls it "ternary" first, with other names including "conditional operator", "inline if" and "ternary if". I'm on Team Ternary though. – tadman Nov 20 '15 at 15:57
  • Wouldn't (test1() || true) always be true or is this different in JS? – Austi01101110 Nov 20 '15 at 17:32
  • @Austi01101110 This is always true, I added || true because if test1() return false, test2 will called. if statement is true, because ( true && false ) = false, and test2 will called, but when I add this trick, if statement is true, and test1 returned false, test2 will not called – Gor Nov 20 '15 at 19:59

3 Answers3

7

It may work the same, but one thing you may want to consider is readability. The first instance of your code is very readable while the second one makes me want to grab a pen and paper and do the math. Speed and readability are trade-offs and unless your program is severely bottle-necked performance-wise, being readable is the better goal.

Lawrence Aiello
  • 4,560
  • 5
  • 21
  • 35
4

You are obviously more comfortable with the if approach since you have many redundant parentheses in the alternative.

Unless the performance differs substantially (which it doesn't in this case), program at 50% of your ability so you can debug at 100%.

I.e. adopt the method you are more content with and find more readable.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
4

The problem with this expression is

(test1() || true)

is that for example function test1 can return type void or an object of a type that is not implicitly converted to type bool as for example an object of a class type that does not have an appropriate conversion operator.

And this record only confuses readers of the code.

There is such a principle in the programming KISS - Keep It Simple Stupid.:)

As it is seen from the first code snippet the intention of the programmer is to call one of the functions.

Looking at the second code snippet it seems that the programmer is going to evaluate a compound logical expression. So this code snippet only arises questions.

It would be more readable if the expression is written

i % 2 == 0 ? ( void )test1() : ( void )test2();

Of course in JavaScript you can write the expression without using void

i % 2 == 0 ? test1() : test2();

It is a requerement of C++.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • @tadman The both operands shall be evaluated to a common type. – Vlad from Moscow Nov 20 '15 at 15:59
  • 1
    @tadman In general the functions can have different types. So using the condition operator for operands that can not be converted to a common type is invalid. – Vlad from Moscow Nov 20 '15 at 16:01
  • @tadman It is not imporatnt whether the result is ignored. In any case the compielr need to determine the common type of the operands to determine the type of the expression. – Vlad from Moscow Nov 20 '15 at 16:02
  • [You're right](http://codepad.org/zT3OuIz5). I'd never noticed that before, but I've never mixed types like this, a when a ternary is involved it's usually matched by design. You'll get a "error: operands to ?: have different types" if they're mixed. – tadman Nov 20 '15 at 16:12