(bCondition == NULL) 和 (NULL==bCondition) 有什么区别?

发布于 2024-11-04 07:43:34 字数 112 浏览 4 评论 0原文

在探索 msdn 站点时,他们使用的大多数条件检查位置 (NULL == bCondition)。

使用这些符号的目的是什么?

请提供一些示例来解释这些。

谢谢。

While exploring msdn sites ,most of the condition checking places they are using (NULL == bCondition).

what is the purpose of using these notations?

Provide some sample to explain these please.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

东北女汉子 2024-11-11 07:43:34

当意外使用赋值运算符 = 而不是比较运算符 == 时,使用 NULL == 条件 可以在出现拼写错误时提供更有用的行为

if (bCondition = NULL)  // typo here
{
 // code never executes
}

if (NULL = bCondition) //  error -> compiler complains
{
 // ...
}

C编译器在前一种情况下会给出警告,许多语言中没有这样的警告。

The use of NULL == condition provides more useful behaviour in the case of a typo, when an assignment operator = is accidentally used rather then the comparison operator ==:

if (bCondition = NULL)  // typo here
{
 // code never executes
}

if (NULL = bCondition) //  error -> compiler complains
{
 // ...
}

C-compiler gives a warning in the former case, there are no such warnings in many languages.

无边思念无边月 2024-11-11 07:43:34

它被称为尤达条件。 (原始链接,您需要高代表才能看到它)。

它的目的是防止在打算进行相等比较 == 的情况下意外赋值 =。如果您习惯性地坚持 Yoda 并通过编写 = 而不是 == 来拼写错误,则代码将无法编译,因为您无法分配给右值。

值得尴尬吗?有些人不同意,他们说编译器在条件表达式中看到 = 时确实会发出警告。我想说的是,在我的一生中只发生过两三次这样的错误,这并不能证明我一生中所写的所有 MLOC 都符合这一惯例。

It's called Yoda Conditions. (The original link, you need high rep to see it).

It's meant to guard against accidental assignment = in conditions where an equality comparison == was intended. If you stick to Yoda by habit and make a typo by writing = instead of ==, the code will fail to compile because you cannot assign to an rvalue.

Does it worth the awkwardness? Some disagree, saying that compilers do issue a warning when they see = in conditional expressions. I say that it simply happened just two or three times in my lifetime to do this mistake, which does not justify changing all the MLOCs I wrote in my life to this convention.

满身野味 2024-11-11 07:43:34

没有什么区别。这是一种古老的防御性编程方式,已经过时了 20 多年。目的是防止在比较两个值时意外键入 = 而不是 ==。迁移到 C 的 Pascal 程序员特别容易编写此错误。

从 1990 年发布的 Borland Turbo C 开始,当您设法输入此错误时,每个已知的编译器都会警告“可能不正确的分配”。

因此,编写 (NULL == bCondition) 并不比相反的做法更好或更差,除非您的编译器非常古老。您无需费心按照任何特定顺序编写它们。

应该烦恼的是适应一种编码风格,在这种风格中您永远不会在if/循环条件内编写赋值。从来没有理由这样做。这是 C 语言的一个完全多余、危险且丑陋的特性。所有行业事实上的编码标准都禁止在条件内进行分配。

参考文献:

  • MISRA C:2004 13.1
  • CERT C EXP18-C

There is no difference. It is an ancient way of defensive programming that has been obsolete for over 20 years. The purpose was to protect from accidentally typing = instead of == when comparing two values. Pascal programmers migrating to C were especially prone to write this bug.

From Borland Turbo C released in 1990 and forward, every known compiler warns against "possibly incorrect assignment", when you manage to type out this bug.

So writing (NULL == bCondition) is not better or worse practice than the opposite, unless your compiler is extremely ancient. You don't need to bother about writing them in any particular order.

What you should bother with, is to adapt a coding style where you never write assignments inside if/loop conditions. There is never a reason to do so. It is a completely superfluous, risky and ugly feature of the C language. All industry de-facto coding standards ban assignment inside conditions.

References:

  • MISRA C:2004 13.1
  • CERT C EXP18-C
西瓜 2024-11-11 07:43:34

许多人更喜欢写 NULL == bCondition,这样他们就不会意外地将 NULL 值赋给 bCondition。

由于打字错误,

bCondition == NULL 

他们最终没有写,而是写了

bCondition = NULL // the value will be assigned here.

In case of

NULL = bCondition // there will be an error

Many people prefer writing NULL == bCondition so that they accidently don't assign the NULL value to bCondition.

Because of typo it happens that instead of writing

bCondition == NULL 

they end up writing

bCondition = NULL // the value will be assigned here.

In case of

NULL = bCondition // there will be an error
屋檐 2024-11-11 07:43:34

这只是一个很好的防御措施。有些人可能还会发现阅读起来更方便。如果输入错误的赋值而不是相等运算符,编译器将尝试修改 NULL,它不是左值,并且会产生错误消息。使用 bCondition = NULL 时,编译器可能会生成有关使用赋值作为真值的警告,该消息可能会丢失并被忽视。

It's simply a good defensive measure. Some may also find it more convenient to read. In case of a mistyped assignment instead of the equality operator, the compiler will attempt to modify NULL, which is not an lvalue and will produce an error message. When using bCondition = NULL, the compiler might produce a warning about using an assignment as a truth value, a message which can get lost and go unnoticed.

爱已欠费 2024-11-11 07:43:34

虽然通常变量==值值==变量之间没有区别,原则上不应该有区别,但在C++中如果涉及运算符重载,有时在一般情况下可能会有所不同。例如,尽管 == 预计是对称的,但有人可能会编写一个并非对称的病态实现。

Microsoft 的 _bstr_t 字符串类在其 operator== 实现中存在不对称问题。

While usually there is no difference between variable == value and value == variable, and in principle there shouldn't be, in C++ there sometimes can be a difference in the general case if operator overloading is involved. For example, although == is expected to be symmetric, someone could write a pathological implementation that isn't.

Microsoft's _bstr_t string class suffers from an asymmetry problem in its operator== implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文