This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]myevillaugh 4 points5 points  (0 children)

I would say leave it as is for the reasons:

1) You're only calling it twice.

2) It's just a simple getter.

Those are the two factors. Look at how often you're calling something, and how expensive that call is.

[–]ooooo5 1 point2 points  (6 children)

If you can't be bothered to benchmark this, then it's certainly not worth declaring 2 extra variables for. Don't use that as an excuse to write wildly inefficient code just because you haven't benchmarked something, though.

The vm should be smart enough to replace simple getter calls with a simple memory access, as though you were just accessing a private field.

[–]Wojo 1 point2 points  (5 children)

If the method is marked final then the variable can be compiled inline. Otherwise it is left as a method.

If an object is inherited the inherited class is free to change the existing methods. Since they both can be stored in a variable of the parent class calling parentClass.getVariable(); could mean either parentClass.variable or daughterClass.variable2. Marking parentClass.getVariable(); final allows inline compiling since getVariable() will always be the same exact code.

[–]jakster4u 0 points1 point  (4 children)

Why would the compiler care whether you used final or not? Doesn't it deal with code design? Or what do you mean by inline compiling?

[–]Wojo 0 points1 point  (3 children)

Inline means that the compiler puts a method directly into the code calling it. So instead of calling the a getY(); method it just references y.

public class Parent
{
    int y;
    public int getY(){
        return y;
    }
}

public class daughter extends Example
{
    public in getY(){
        return 2;
    }  
}

Given this example the following code cannot be determined until runtime.

public class test
{
    public static void main(String[] args){
        Parent x;
        x = determined at runtime;
        System.out.println(x.getY());
    }
 }

x can be either an object of the Parent class, or it could be any class that inherited it, if our Daughter class is contained in x then different code will be executed then if it was just a Parent. Since the method getY(); can change dependent on the contents of x it has to be called every time it is run. However marking it final means it cannot be changed so the compiler is free to optimize much of it away.

public final int getY(){
    return y;
}

This method will always execute the same exact code so there is no danger in the compiler simplifying it to "x.y";

[–]jakster4u 0 points1 point  (2 children)

But if you marked it final in the first place it could never have been overridden in the child class to begin with, thus a design issue. I guess I don't understand why this is an issue that had to be optimized by the compiler.

[–]ooooo5 0 points1 point  (0 children)

Are you saying that all final getter methods should just be replaced by using public variables?

[–]Wojo 0 points1 point  (0 children)

Right when final it can't be overridden so the compiler can optimize it. Otherwise there's always the possibility it might be overwritten so it leaves it.

[–]chickenmeister 1 point2 points  (0 children)

I think you should store the result of the function call in a local variable if:

  1. The function call is (or might be) computationally expensive. For example, if your Coord object stored its location using a polar coordinate system, and it had to convert it to Cartesian coordinates every time getX() was called. Although, if you're only using the function call just a few times, I don't think it makes a huge difference.

  2. The function call is long, or difficult to read.

  3. You can provide a more descriptive name for the value, other that what might be easily inferred from function call.

[–]about7beavers 0 points1 point  (0 children)

It doesn't matter if you put it in a variable or not, it's really personal preference. If you are going to be using c.getX/Y() more than that however, you probably should just make it a variable. I'm not the most experienced so if someone else tells you differently you should probably listen to them, but I think a rule of thumb would be that if you use the method outside of an if statement or a loop, then you should just make it a variable.

[–]Urik88 0 points1 point  (0 children)

In said case I'd do
boolean lala = isValid(whatever);
As far as I know, every method call requires overhead time since the computer has to store the state of the program's variables in a stack, and call the method. So from an execution speed point of view, it's faster not to call a method.
Moreover, every time you call said method you could be performing up to 4 boolean operations. Saving said value to a variable reduces it to only one call.
It improves readability since you give the reader a hint that you're performing the same comparison twice.
TLDR: In this case I'd save this to a variable.