Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if(false==condition). Why? [duplicate]

Tags:

c++

I have received code from someone working earlier on it, and it contains a lot of lines like

while(false==find && false == err && k<kmax)
if(true==refract(ep1,ep2,n1,RI_blood, RI_collagen))

and my favorite line is

if(false == (ret_s<0))

The other code is done really well, documented just fine, but these lines with these odd conditions are throwing me off, and I wonder why they are done that way.

Especially that false==(ret_s<0) is completely confusing, and you kind of need to read that line like three times to understand what they want there.

Is this a common programming style, don't I understand the reasoning for that, or is that just bad style?

Edit: I don't feel this is similar to if(object==NULL) vs if(NULL==object), since this isn't about accidental assigning but about obfuscated if clauses...

like image 782
SinisterMJ Avatar asked Jun 03 '13 20:06

SinisterMJ


People also ask

What happens if we use false inside if condition?

In an if...else statement, if the code in the parenthesis of the if statement is true, the code inside its brackets is executed. But if the statement inside the parenthesis is false, all the code within the else statement's brackets is executed instead.

Does if false execute?

Our if statement's condition should be an expression that evaluates to TRUE or FALSE . If the expression returns TRUE, then the program will execute all code between the brackets { } . If FALSE, then no code will be executed.

Which block will be skipped if the condition is false?

If the code in the if block is False , it will move to the elif block. If that is True , then the rest of the blocks are ignored. If it is False , Python will move to other elif blocks if there are any present. Finally, if all of the conditions are False , then and only then the code in the else block will run.


2 Answers

Is this a common programming style?

No.

don't I understand the reasoning for that?

Some people like to explicitly compare booleans with true or false, even though the result is exactly the same boolean value. The logic is presumably that, by making the code harder to read and more surprising, people will think harder about it and make fewer assumptions about its behaviour. Or perhaps just that code should be hard to maintain, since it was hard to write.

Others like to write comparisons with constants backwards, which prevents mistakes like if (x = 5) when you meant if (x == 5). Any modern compiler will warn you about this mistake, so again its only real purpose is to make the code harder to read.

Combining these two behaviours gives the bizarre code you posted.

Or is that just bad style?

It's a style. I'm no judge of style, but if you like to keep maintainence programmers on their toes, it certainly does that. Personally, I like my code to be readable, but that's just me.

my favorite line is

I once encountered return a && !b implemented in about ten lines of code. The first line was switch(a).

like image 99
Mike Seymour Avatar answered Nov 07 '22 10:11

Mike Seymour


Yoda Conditions

enter image description here
Using if(constant == variable) instead of if(variable == constant), like if(4 == foo). Because it's like saying "if blue is the sky" or "if tall is the man".

like image 42
Chad Avatar answered Nov 07 '22 10:11

Chad