I am trying to make a report of some code enhancement, however, i am not too sure what this item is called. Basically, instead of doing a conn == null, it does a null == conn for readability.
Before:
if (conn == null){
if (log.isDebugEnabled()){
log.debug("Failed to get a DB Connection");
}
}
After:
if (null == conn){
if (log.isDebugEnabled()){
log.debug("Failed to get a DB Connection");
}
}
It’s not for readability, it’s to prevent accidental assigning instead of comparing.
If you accidentally write:
in C or C++, that will set
conntonulland then use that value as the condition. This has been carried over into Java despite the fact that it’s not necessary there.The other way around, the compiler catches it as an attempt to assign to a constant so it won’t even compile.
As for what its called, I’ve never seen it given a specific name. You can call it what you want provided you explain it. I like
Accidental assignment avoidancesince I can just shorten that totriple-a.