Which do you prefer?

Option 1


int x = getX(),
    y = getY(),
    z = getZ();

Vector v = new Vector(x, y, z);

Option 2


Vector v = new Vector(getX(), getY(), getZ());

I personally prefer option 1. Code is harder to read if more than one method is called in the same line / statement, unless the code is really short. However, I cannot say that I enjoy the longer code. Really makes me wish java had C#'s properties.

I prefer choice 2. It should theoretically be slightly quicker, however if you then wanted to retrieve x, y and z through the object Vector it would likely end up being slower. So, really it would depend on how those variables are going to be used, and I’d probably end up with choice 1.

choice 2. In this case, it’s totally clear what happens - and having local variables doesn’t give you any benefit here… as a Java guy, you learn to read getX() as if it was x… so no problem here for me :stuck_out_tongue: The interesting part is the vector…I’m not interested in how it is built exactly. It’s enough that it is there.

If this really insults you, take a look at project lombok, although it’s controversely discussed - using it for property support should be fine.

I think you guys are slightly missing the point. There is no notable performance gain in either option. The compiler will most likely make them the same code anyway. The only difference is style. Option 1 is objectively clearer. Since there functions are more spread out in option 1, there is simply less than worry about per line. However, option 1 also has more lines. There is a trade off. Even C#'s properties has its own trade-offs. Since no method is inherently better than other, it is a matter of taste. Therefore, what are your in regard to this matter? Reasoning would be appreciated although I nothing more than “it feels better.”

[quote=“tariqbroadnax,post:4,topic:58470”]
Sais who - you?

“Clear” is, that the main purpouse of the code should be the construction of a vector with x, y and z property. This is happening in line 5. The other lines are clutter, so there’s nothing more clear, but more unimportant code that distracts you from the main purpose.

But I agree with your other statement: It’s a matter of taste. But that’s why the creator of this thread asked: Because he searches for advantages and disadvantages of the two options.

In this case, I tend to say, that the logic is placed in a helper method “createVector” either way, so the code itself is rarely or even never used/placed somewhere outside of such a method…that means bloating the code up with local variables happens exactly once - which seems to be okay for me, because it might be easier readable for someone who has never touched the code at all.

Option 2 is clearer and simpler in the case where you have no other uses for x, y, or z.

If you subsequently need to use x, y, or z somewhere further down the line, you’d go for option 1.

Cas :slight_smile: