Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2006

Tip: Looking for answers? Try searching our database.

Local variable or not?

Thread view: 
Hendrik Maryns - 01 Feb 2006 13:33 GMT
Hi,

I realise that using a local variable for some object you need multiple
times in a method is useful, especially if it is computation intensive
to compute that object (or value).  I was just wondering whether this is
still the case if the method for getting to that object is a simple
getter which does only return an instance variable (immutable, no copy
needed).

Concrete:

class Local{
    private Object example = new Object();
    public Object getExample(){
        return example;
    }

    public void someMethod(){
        int someInt = getExample().hashCode();
        Class someClass = getExample().getClass();
        // more instructions, invoking getExample()
    }

    public void equivalentMethod(){
        Object ex = getExample();
        int someInt = ex.hashCode();
        Class someClass = ex.getClass();
        // more instructions, using ex
    }

}

Which of those two methods would be preferable?

Thanks, H.
- --
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
Thomas Weidenfeller - 01 Feb 2006 13:48 GMT
> Which of those two methods would be preferable?

It depends, among other things, on

* The behavior of the getExample() method. E.g. it might return a
different object every time called. Which might or might not be
desirable. And

* side effects of the getExample() method. E.g. it might calculate or
update some internal value every time called, print some message, update
some statistics counters, notify some other component etc.

* The amount of computation to do on the result returned by getExample().

* Possible Threading-related operations (e.g. locking) required to do on
the result returned by getExample()

In short, you are looking for a general rule where there is non in
general. You hold an own reference to an object when you have a need for it.

/Thomas

> Thanks, H.
> - --

PS: You might want to change that broken signature delimiter in front of
your novel-size signature to a valid one.
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Monique Y. Mudama - 01 Feb 2006 16:36 GMT
>> Thanks, H.
>> - --
>
> PS: You might want to change that broken signature delimiter in front of
> your novel-size signature to a valid one.

Er, I only saw a four-line signature for Hendrik.  Certainly shorter
(at least in terms of bytes) than your links.  Am I missing something?

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Thomas Weidenfeller - 02 Feb 2006 09:01 GMT
>>> Thanks, H.
>>> - --
[quoted text clipped - 3 lines]
> Er, I only saw a four-line signature for Hendrik.  Certainly shorter
> (at least in terms of bytes) than your links.  Am I missing something?

The signature delimiter is '--<space><newline>', not '-<space>--<newline>'.

Therefor news readers who are looking for the delimiter to e.g. to cut
of the signature when quoting or displaying a message can't do so. You
get the signature (which then isn't technically a signature) as part of
the message.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Hendrik Maryns - 02 Feb 2006 12:24 GMT
Thomas Weidenfeller schreef:
>>>> Thanks, H.
>>>> - --
[quoted text clipped - 10 lines]
> get the signature (which then isn't technically a signature) as part of
> the message.

I know this, and my signature is inserted automatically by Thunderbird,
correctly.  Unfortunately, the EnigMail extension which I installed
recently adds a PGP signature, and to be able to do so has to add this
extra hyphen.  Interestingly, if you read the message with TB having
EnigMail, it gets shown and cut off correctly, but it is a problem if
you don?t have EnigMail.  I will look into this and perhaps file a bug
with the EnigMail developers.

Thanks for the answers.

Indeed I am not too much concerned about performance, more about good
style, but was just wondering how much of a method overhead is still
acceptable before you should use a local variable (the example I gave
obviously does not cause much overhead).

Cheers, H.

- --
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
Thomas Weidenfeller - 02 Feb 2006 13:44 GMT
> Indeed I am not too much concerned about performance, more about good
> style, but was just wondering how much of a method overhead is still
> acceptable before you should use a local variable (the example I gave
> obviously does not cause much overhead).

When your application gets to slow, and a profiler indicates that the
usage of repeated getSomething() calls is the bottleneck :-).

Before that happens I would first be concerned about side effects of the
call and second be concerned about aesthetics (particular because of
maintainability).

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Lion-O - 02 Feb 2006 16:32 GMT
>> The signature delimiter is '--<space><newline>', not '-<space>--<newline>'.

> Unfortunately, the EnigMail extension which I installed recently adds a PGP
> signature, and to be able to do so has to add this extra hyphen.

PGP or GPG ?  If you're using GPG then you can use the "--not-dash-escaped"
option on either the commandline or in your config file.

Just a quick, offtopic, comment ;)

Signature

Groetjes, Peter

.\\ PGP/GPG key: http://www.catslair.org/pubkey.asc

Monique Y. Mudama - 02 Feb 2006 16:28 GMT
>>>> Thanks, H.  - --
>>> PS: You might want to change that broken signature delimiter in
[quoted text clipped - 11 lines]
> so. You get the signature (which then isn't technically a signature)
> as part of the message.

I know what the standard sig delimiter is.  I was responding to your
claim that he had a "novel-size" signature.  Unless my slrn config is
stripping away excess crud, which it might be, I don't see how you
could claim his signature is lengthy.

As for the sig delimiter, I absolutely agree that people should use
the standard.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Ed - 01 Feb 2006 15:13 GMT
Hendrik Maryns skrev:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 4 lines]
> times in a method is useful, especially if it is computation intensive
> to compute that object (or value).
Snip
> -----END PGP SIGNATURE-----

Thomas makes some excellent, technical points, so I'm not going to.
Some do prefer, however, using the local variable if there are no great
forces pressing against it. Using a local variable just seems somehow
... purer than all those getExample() methods. Entirely a subjective
choice, of course (but no less important for that; few forces unleash
as much power as the urge towards the aesthetic).

.ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
Paulus de Boska - 01 Feb 2006 20:49 GMT
Hendrik,
calling a method to obtain a reference is always more expensive than
using the reference that you already have. So, depending on how many
times you need the reference to call methods on, perhaps in some
extensive loop, you may be better of by getting it once and using that
repeatedly.

---
Paul Hamaker, SEMM
http://javalessons.com
Tony Morris - 01 Feb 2006 22:01 GMT
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 47 lines]
> =3PD2
> -----END PGP SIGNATURE-----

Yet another poor example of designing for performance. No doubt you will
receive many contradictory responses. If invoking the method is more
appropriate - for example, it provides a meaningful abstraction - then do
so. Method invocations are inlined at some point (after a static
configurable number of invocations iirc - I rarely work with the Sun VM), so
the performance argument, even if was sensible, is still not sensible (as
per norm).

Signature

Tony Morris
http://tmorris.net/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.