in a tutorial (for implementing a xml parser) i saw the following code:
if( "NODENAME".equals(xmlreader.getNodeName()) ) { // getNodeName() returns java.lang.String
...
}
Is there a reason to write a string comparision like that?.
This may be some kind of best/bad practice or code like that could give some performance benefit. I would like to know if i should use this in commercial projects.
That saves you from a NullPointerException.
That is Yoda Condition used to solve unsafe null behavior.
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.
Advantage is
Swapping the two conditional values does not change the behavior of the program. A common mistake is to accidentally assign a value instead of writing a conditional statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With