In Java, it's allowed to have assignment in a if condition, meaning we can have code like this:
List<T> result;
if ((result = getResultFromA())) == null) {
result = getResultFromB();
}
return result;
Which I think is equivalent to:
List<T> result = getResultFromA()
if (result == null) {
result = getResultFromB();
}
return;
I know the second one is more readable, but I'm wondering if there is any difference in terms of efficiency or performance? Is the first style a bad practice? I would appreciate any ideas.
Update: Thanks for all these great ideas. Actually the question should be: which of these two syntaxex should be followed? Apart from readability, is there any other reason why we prefer the second style?