This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

what is "better" ...

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i was just wondering what would be more efficient :

or

[ July 15, 2003: Message edited by: Andrew Lit ]
 
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that it is preferable to avoid the overhead with exceptions when possible; therefore, the first alternative would be preferred.
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I guess the first option is better becasue there is minimal overhead involved as said above.I agree with him.
ATB,
Gaya3
 
Sheriff
Posts: 67601
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd say the first option is preferable since it is more readable. Screw any overhead considerations.
bear
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with BB - readability is far more important. But it's a nice coincidence that the readable idiom is also faster. (Unless you have a lot of tokens, and even then the difference is very minor.)
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm, I'd never use the ? operator and "more readable" in the same example. But that's just me.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, for folks like Stan I recommend:

or

or even

I don't care much which is used (though I'm bemused at any code that uses ten lines when two will do). Any of these is preferable to catching the NoSuchElementException, IMO.
 
Joel McNary
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for adding that last one, Jim. That's the one I can read -- though I save two lines of code by placing the open curly-braces on the ames lines as the ifs/elses. So I only use eight lines of code when two would do.

(Although personally, I'm comfortable reading ? : syntax, so in the real-world I'd only need two lines....)
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One additional consideration for the choices here (or a more generalized rule).
You should really only catch errors for conditions that might occur during runtime that you can't anticipate in advance. In this case, you already know that you will at some point run out of tokens, so you should test first instead of catching an error.
 
reply
    Bookmark Topic Watch Topic
  • New Topic