空检查编码标准标准

2023-09-04 13:39:56 作者:憧憬

我有一个关于编码一个空检查标准的一个疑问。 我想知道的。

I have a doubt regarding coding standard of a null check. I want to know the difference between

if(a!=null)

if(null!=a)

哪一个更好,使用哪一个,为什么?

which one is better,which one to use and why?

推荐答案

两者都是在Java中一样,因为只有布尔EX pressions可能是内部的,如果这仅仅是一个编码风格通过编程器preference,其中大部分使用空!= A

Both are same in Java, as only boolean expressions can be inside an if. This is just a coding style preference by programmer and most of them use null != a.

空!= A 是旧做法在如Java,C ++编程语言(称为尤达条件)。 因为它是有效的写若(a = NULL)时,不慎分配一个所以写首先是一名警卫阻止这起事故的发生。

The null != a is an old practice in programming languages like Java,C++ (called as Yoda Conditions). As it is valid to write if (a = null) and accidentally assign null to the a so writing null first is a guard to stop this accident from happening.