问题描述
我刚刚发现我可以像这样比较 null
和 Object
$ b
if(null!= Object)
$ c> Object 与 null
,例如
Object!= null
如果使用前一种方法,可能会出错?
这是合法吗?如果没有,为什么编译器接受它?
如果你想写一个干净的代码,你应该关心它的方式,将在未来读。它需要是明显的,它做什么,为什么它做某一件事。如果你把对象放在评价的右边,你真正做什么就变得不那么明显了。至少在我看来...
I just found out that I can compare null
with an Object
like this,
if(null != Object)
Rather than comparing Object
with null
, like
Object != null
What may go wrong if use the former approach?
Is that legal? If not then why does compiler accept it?
There's one thing wrong about it - readability. If you want to write a clean code, you should care about the way it will be read in the future. It needs to be obvious, what it does and why it does a certain thing. If you place the "Object" to the right of the evaluation, it becomes less apparent what are you really doing. Well at least in my opinion...
这篇关于将null与对象进行比较,而不是将对象与null进行比较有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!