Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Backwards" Conditionals in C [duplicate]

Tags:

c

conditional

I'm looking through some code and I found some strange conditionals, namely:

if (NULL != buf) {...}

I was wondering if there was a particular reason for writing the conditional like this, instead of

if(buf != NULL){...}

I can't see any reason to do it the first way off the top of my head, but I don't think it was a mistake. To me, it seems like they accomplish the same thing, but the second way is way more intuitive. Is there some specific reason to use the first conditional?

like image 488
Justin Wang Avatar asked Dec 05 '22 03:12

Justin Wang


1 Answers

Yes, it's called "Yoda conditions". The idea is to not accidentally assign a value when you mean to be doing a check. Most modern compilers should catch it.

like image 115
rost0031 Avatar answered Dec 20 '22 17:12

rost0031