5

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;
}
3
  • @Sanjay T. Sharma agreed Commented Jun 14, 2011 at 16:52
  • It's probably worth it to change the signatures to be prototypes for functions rather than constructors.. Just saying. Commented Jun 14, 2011 at 16:54
  • @Mike means adding a return type. Commented Jun 14, 2011 at 16:59

8 Answers 8

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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.

Comments

1

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.

Comments

0

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.

Comments

0
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 ....
}
}

Comments

0

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.

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.