Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument comparison order

I'm a seasoned programmer and therefore feel a little embarrassed asking this question but decided to do so anyways.

I'm reading this book, and have also seen various examples across the net where the order of arguments in a compare operation is reversed and I wonder if there is a reason for it, or just because it looks "cool" and does the exact same thing.

Example:

I would code:

if(bool_variable == YES)

while I saw in this book and in various examples

if(YES == bool_variable)

Explanations?

Thanks a ton!

like image 384
schone Avatar asked Nov 19 '25 21:11

schone


1 Answers

Some people like yoda conditions because they can help you find errors where you accidentally type = instead of ==. For example:

if (var = YES)

will compile but probably not work the way you want it to. Writing it the other way around:

if (YES = var)

will cause a compile-time error. I personally dislike this construct, but to each his own, I guess.

like image 68
Carl Norum Avatar answered Nov 22 '25 18:11

Carl Norum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!