• 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

Is it costly to do req.getParameter() multiple times??

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
Please have a look at the statement below? Do u think there is an overheade in the statement (since req.getParameter() is done twice when the paramenet is not null).
String sSearchBy = req.getParameter("SearchBy")==null?"" String)req.getParameter("SearchBy");
Is there a better way to optimize the code??
Thanks,
Sajee Joseph
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You wrote: String sSearchBy = req.getParameter("SearchBy")==null ? "" : ( String)req.getParameter("SearchBy");
Sure, there's plenty of ways to improve this, for example:

Personally, I don't particularly like to hard code the default value ("") in situations like this, so I'd probably take a few minutes to write something like:

The in my code I could use the more expressive:

The biggest advantage of this approach is that it makes it so simple to appy a default value when reading parameters, that I'm much more likely to consider doing it every time, and not risk risk run-time failures through laziness.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic