my dog learned polymorphism
The moose likes Servlets and the fly likes Is it costly to do req.getParameter() multiple times?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Is it costly to do req.getParameter() multiple times??" Watch "Is it costly to do req.getParameter() multiple times??" New topic
Author

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

Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
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
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
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.


A Convergent Visionary ~ Frank's Punchbarrel Blog ~ LinkedIn profile
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Is it costly to do req.getParameter() multiple times??
 
Similar Threads
Servlet Request (Integer)
A page not found error
doubt regarding default values
A null pointer that I dont quite understand
prepared statement