For example:
public Person newPerson() {
Person p = new Person(\"Bob\", \"Smith\", 1112223333);
return p;
}
as opposed to:
<
In a sane world, they will compile to exactly the same bytecode, so they are identical as far as performance is concerned. Then the only difference is for humans:
Creating a temporary variable makes debugging slightly easier: you can put a breakpoint on the return statement and inspect the value of p
. In the one-line version, this is harder to do.
Doing it in one line eliminates a variable, reducing complexity, making the code slightly easier to read.
In practice, I would write it in one line, and in the worst case I create a temporary variable if I run into the debugging issue. Besides, aren't unit tests supposed to eliminate the need for debugging? :-)