“倒退"C中的条件 [英] "Backwards" Conditionals in C

查看:20
本文介绍了“倒退"C中的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看一些代码,我发现了一些奇怪的条件,即:

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?

推荐答案

是的,它被称为Yoda 条件".这个想法是在您打算进行检查时不要意外分配一个值.大多数现代编译器都应该捕捉到它.

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.

这篇关于“倒退"C中的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆