PDA

View Full Version : Is using Request or Response objects from helper methods appropriate?


Grant Palin
05-20-2004, 06:44 PM
I have a main method in my ASP page to process user input from a form. I delegate some of the repetitive work to helper functions or subs. In those helper methods, is it appropriate to use the Request or Response objects (Request.QueryString, Response.Write)?

I am trying to simplify my code as much as possible. I know the importance of code reuse and cohesion and so on. In the form processing method, should I get necessary values from Request, and pass those local values to the helper methods? Or is it okay for the helper methods to interact with the Request object directly? Same for Response; can I just output directly from the helper methods, or should I return the necessary information to the main methods to output themselves?

If it makes a difference, I'm doing output from a database, and am thinking about doing a seperate method to create queries for various combinations of data. From that method, I thought to call a helper method to handle the actual query execution and output.

Am I on the right track, or do I need to rewrite my code some?

Woohoo! Passed 500 posts!

shmoove
05-20-2004, 08:40 PM
As far as code reuse goes, using the Request object might not be such a good idea. You might want to use the function in another place where the input doesn't come from the Request, or is passed in a different parameter. You can easily change the function to get those inputs as parameters to the function. With Response it's a slightly different story, since the Response will always be the same. Unless you think you might want to use the function and put the result in a database, or use it as a parameter to another function, in which case you might want to change it to a return value. But since there can only be one return value (as opposed to as many parameters as you need), it might be harder. If you can it definitely wouldn't hurt.

shmoove

Roy Sinclair
05-20-2004, 09:06 PM
A sub is still a sub whether it's part of the main file or included via a SSI so I don't think you need worry about using either the response or request objects within those subs any more that you'd worry about using them in a sub that's part of the main file. Likewise those objects are global in scope so using them inside a sub isn't going to cost much more than using them outside a sub (there should be a slight difference since the local scope should be checked first).

Grant Palin
05-21-2004, 01:37 AM
Thanks for the advice, both of you. I decided to employ somewhat better programming practice by avoiding global variables (e.g. Request.QueryString) except in my main form-processing method. I've seperated some common functionality into functions in a seperate file. When sending data tot hose helper functions, I just use parameters - no globals. In my output-creating functions, there is no direct output. I just keep appending HTML strings to a single string, and return that string.