• 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

question about declaring extraneous variables

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Often times I come across code as such...

boolean isValid = someObject.isValid();
if (isValid) { // isValid only being used in one place
....
}

other than the fact that the variable is declared outside of the block in which it is being used, is there a performance hit (no matter how small) to doing this or does the compiler figure it out and optimize the call

This question does not apply to those cases where readability is to be take into account such as...

double commissionAmount = someObject.someReallyScrewyMethodName();
if(commissionAmount > 15.43) { // used once - var for readability only
...
}

Thanks, in advance
 
(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 often make the extra variable as I think things out and then decide later whether or not to eliminate it. I usually make them go away unless the next line gets too long to read.

Every variable is a floating hazard to navigation in the rest of the scope. Somebody might decide to use it for something else. It's also one more unit of information the reader must comprehend and then go find all references to for understanding. These are tiny negatives, but some people are quite sensitive about them. If you decide to keep it, making it final would express your intent that it's only set once.

And if you're able, change the ridiculouslyLongMethodName to something meaningful but easy to digest. The name you were about to give the variable might be a good candidate.
[ November 16, 2007: Message edited by: Stan James ]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
on the other hand, nothing makes me scream louder than seeing something like:


foo.method1().method2().method3().method4().method5();

or

foo.method1(bar.method2(oof.method3(rab.method4)));

in these cases, i'd break each method out and store what it returns in a temp. variable, every time.
 
Stan James
(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 with you, Fred. I have a limit of about 2 on things like that. But some of the DSL language folks seem to love the chained methods.
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic