Yoda conditions have been born to avoid hard-to-catch mistakes, like an assignment in a condition statement. If variable is null in your first example you catch it immediately, because it throws a NullPointerException as you mentioned.
So it doesn't save you from a mistake; it saves you from checking if the value is null or not. And honestly I'm not sure it's what I'd want: if I expect a string there, why am I dealing with null?
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Can you really call it a "Yoda condition"?
Yoda conditions have been born to avoid hard-to-catch mistakes, like an assignment in a condition statement. If
variable
isnull
in your first example you catch it immediately, because it throws aNullPointerException
as you mentioned.So it doesn't save you from a mistake; it saves you from checking if the value is
null
or not. And honestly I'm not sure it's what I'd want: if I expect a string there, why am I dealing withnull
?