(null == var)和(var == null)之间有什么区别吗 [英] Is there any difference between (null == var) and (var == null)

查看:104
本文介绍了(null == var)和(var == null)之间有什么区别吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道if语句中两个条件表达式 null == var var == null 之间的差异Java

I want to know differences between two conditional expressions null == var and var == null in if statement of Java.

推荐答案

没有区别。只是风格问题。

There is no difference.Simply a matter of style.

第一个名为 yoda style


在编写术语时,Yoda条件(也称为Yoda表示法)是一种编程风格,其中表达式的两个部分在条件语句中相反。

In programming jargon, Yoda conditions (also called Yoda notation) is a programming style where the two parts of an expression are reversed in a conditional statement.






虽然两个条件表达式 null == var var == null 是equvilent。但是假设你把misspell == (检查相等)的bug引入 = (赋值)那么 var = null 不会给您带来错误并在您的代码中引入错误。而你写 null == var 并假设misspells null = var 它会产生编译时错误。编译时错误比运行时错误更好。这是尤达条件形式的优势之一。


Although both conditional expressions null == var and var == null are equvilent. But suppose if you introduces bug by misspell == (check equality ) as = (assignment) then var = null doesn't give you can error and introduce a bug in your code. Whereas you write null == var and suppose misspells null = var it produce a compilation time error. And a compilation time error is a way better than a runtime error. That's one of the advantages of the Yoda conditional form.

但我也建议你阅读批评

许多程序员讨厌它,因为它必须在精神上重新反转它才能理解它。 (其他人显然没有这个问题)。这种做法被称为尤达条件。无论如何,大多数编译器都可以被说服以警告条件中的任务。

Many Programmers hate it, as it has to mentally re-reverse it to understand it. (Others obviously don't have that issue). The practice is referred to as "Yoda conditions". Most compilers can be persuaded to warn about assignments in conditions anyway.

这篇关于(null == var)和(var == null)之间有什么区别吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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