• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Local variable vs method call

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

suppose i have a method (e.g. getA() ) which returns object of type A. Which of the following two approach will be more efficient:

getA().set1("");
getA().set2("");
getA().set3("");
getA().set4("");

or

A a = getA();
a.set1("");
a.set2("");
a.set3("");
a.set4("");

Thanks in advance


=====
KALAI
[ April 19, 2006: Message edited by: Kalai Selvan ]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More efficient in what?
- Memory usage
- CPU usage
- Programming time
- Maintainability

The first 2 you can test yourself in 5 minutes, as for maintainability and such i would prefer the second way, but some may not.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's also possible that the two pieces of code may do something different (for example if getA() returns a different A object every time). In which case the question would not make sense.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
It's also possible that the two pieces of code may do something different (for example if getA() returns a different A object every time).



And if it doesn't, it might well be that they are identical in performance, because the JVM decides to inline the method call. And even if it doesn't, the difference in performance is unlikely to have any noticable impact.

Still, I'd typically prefer the second version, because I find that it's easier to read. But that is more of a personal preference than a hard rule.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also notice that with a modern IDE that includes a refactoring browser, switching between the two versions is a matter of two mouse clicks.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm at a point in life where any repetition and duplication bothers me, so I'd be bugged by four calls to getA() more than by any performance conerns. Your priorities in defining "good" may be different.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getA() would do pointer dereferencing,
so I would suggest doing

A a = getA();
a.method();
a.method();

because it's generaly easier to maintain, and the
code block only dereference once, then kept the
reference for later use in 'a'

to be exact, I would use one over the other depending on the code block.
i.e:

// no need to grab A, coz it's pointless
public String method aaa_simple()
{
this.B.getA().getAddress();
}

// grab reference to A, makes life easier
public String method aaa_long_andComplex()
{
A a = this.B.getA();

String somethingGenerated;
// do some operation with a.getAddress()
// some more operation with a.getWhatever()
// some more operation with a.getWhatHaveYous()

return somethingGenerated;
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic