Reputation: 525
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;
}
Upvotes: 5
Views: 215
Reputation: 13728
Since the getXXXX() is usually a naming convention for an accessor method, it will be part of your class, so something like this would suffice:
public double getHeightInMeters() {
return this.height*x;
}
If you need a mere converter, name it differently and pass a double, since it is doing nothing with the Object you are passing. This way you can reuse this method for any other context too.
Keep things simple!
Upvotes: 0
Reputation: 30875
That generally depends:
IF you are using more then one property of the object the first solution is better than adding more parameters. This have also sense if the operation is related only to object otherwise use the second form if this is only some atomic operation that can be performed on other objects either.
Another thing it hat get
prefix should be generally used for class members, in this case it seams like it only operate on some constant and extern value. We don not the unit of it. Better naming would be for example pixelsToMetters(int pixel)
, in this case we know both units.
Upvotes: 0
Reputation: 5007
class Person {
private double height;
public void setHeight(double height){
this.height = height;
}
public double getHeight(){
return height;
}
}
The convention might be that height for this class is in meters. But you may create some convertes for other metrics
public class Converter{
public static double convertFromMeterToInch(double meters){
return meters * 'some factor you get from net'
}
public static double convertFromInchtoMeter(double inch){
return ....
}
}
Upvotes: 0
Reputation: 7926
Passing just the height information requires less overhead than the entire object, as you have less data to serialize each time. For a small app it makes no difference, but as you scale up you will incur a larger overhead.
It is the better option if that is the only property you need from the object.
Upvotes: 0
Reputation: 45599
The second one is the best/proper way.
It's better to pass in the property height, because you break down the dependency. That is, if you change the passed in object, delete/rename method, you will end up with huge head-aches.
Furthermore, if you just pass in the property height, you will always know what goes in and what comes out.
Upvotes: 1
Reputation: 103155
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.
Upvotes: 2
Reputation: 40871
The second. Does getHeightInMeters care anything about object? If not, then it doesn't need it.
All it needs is Height, so that's what you should pass it.
Upvotes: 2
Reputation: 4263
The second version is better. I has a less complicated signature. Since the getHeightInMeters() method only needs the height value, you should keep it simple.
Upvotes: 2