0

I have a method that return an object and I need to call multiple times two methods. All of this has to be written in a performed action event.

Which one according to you is the better solution? Which one consume less resources and it's faster?

public void actionPerformed(ActionEvent e) { 
    Object obj1 = getObject().getSomething1();
    Object obj2 = getObject().getSomething2();

    doSomething1(obj1);
    doSomething2(obj2);
    doSomething3(obj1);
}

or it's better

public void actionPerformed(ActionEvent e) {
    doSomething1(getObject().getSomething1());
    doSomething2(getObject().getSomething2());
    doSomething3(getObject().getSomething1());
}
Logan
  • 926
  • 1
  • 10
  • 18
JessicaJ
  • 31
  • 5
  • 2
    This is opinion based. I'll say though, I prefer having intermediate data stored in variables in many cases. It makes debugging easier since then you can just inspect the value of the variable. In terms of operation of the program though, I'd expect them to be optimized to the same code in most cases. – Carcigenicate Jun 13 '18 at 19:11
  • 3
    I believe the compiler converts the first version into the second anyway, so runtime performance won't be affected. – varontron Jun 13 '18 at 19:11
  • " Which one consume less resources and it's faster?" The first. The question is *how many more* resources, *how much* faster, and is it actually significant. – Andy Turner Jun 13 '18 at 19:12
  • @varontron I am not so sure about that. I doubt that the java compiler is able to recognize methods without side-effects and therefore perform CSE on those method-calls. – Turing85 Jun 13 '18 at 19:13
  • 1
    @varontron no, the compiler certainly won't; the JIT *might*, just about, if after a while it can work out a lack of side effects. – Andy Turner Jun 13 '18 at 19:13
  • @AndyTurner, yes you're right. – varontron Jun 13 '18 at 19:13

2 Answers2

0

I would say that this is never something that a computer programmer should worry about without first being prompted. You should always concern yourself with what is more readable.

If you run your code and come into a problem regarding performance then you go back and check if there is a significant performance difference. At any rate, I believe you won't find one. Modern day compilers are very good at optimizing things like this.

Aelphaeis
  • 2,593
  • 3
  • 24
  • 42
  • To those flagging this answer for deletion. The answer can be rephrased to, whichever is better is whichever you find more readable. This is a perfectly valid answer. – Aelphaeis Jun 20 '18 at 13:16
-1

In terms of performance, it depends mainly on the getSomething1() processing. If no computation or a light computation is performed, the two ways should have broadly the same performance.
If the processing is more heavy, you may have some differences but there still it depends on the number of invocation performed in the frame of the application.

Beyond performance that is actually not an issue in your case, you should first focus on other fields such as potential side effect and code quality.
Side effect : does the method generate a side effect that you don't want to repeat ? In this case, ensure that you invoke it a single time.
Code quality : the code duplication should generally be avoided and the second one does that but meanwhile introducing more variables than required should also be avoided. So which way is the better ? I think that we should have the whole code to argument in a way or in another way.

Note that you can also choose a third option by introducing a method accepting the object : it avoids duplication and it limits the number of declared variables :

doSomething(getObject());

private void doSomething(Foo foo){
   foo.getSomething1();
   foo.getSomething2();
   foo.getSomething1();
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215