I have kind of a general java question I’m looking for an answer to. Lets say I have an object with a property height and I have a method that uses height to make some calculation. Is it better to pass the property height to the method or is it any different to pass the full object and use a getter to retrieve the value of height. I hope this makes sense.
e.g.
public getHeightInMeters(Object object) {
return object.getHeight()*x;
}
is the same, worse, better than?
public getHeightInMeters(Height height) {
return height*x;
}
It depends.
If the operation that you are performing is semantically linked to the type of object then it makes sense to pass the object. Or if you are using more than one properties of the object.
If the operation is generic, that is, it applies to an integer rather than to a specific property of the object then just accept an integer.