"向后"用C条件语句
我期待通过一些code和我发现了一些奇怪的条件语句,即:
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?
是的,这就是所谓的尤达条件。我们的想法是,当你的意思是做了检查,不小心分配一个值。大多数现代编译器应该抓住它。
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.