I used to heavily rely on session variables in the past, but have recently found
ID: 651159 • Letter: I
Question
I used to heavily rely on session variables in the past, but have recently found many of them to be unnecessary, using things like query string parameters instead.
A colleague of mine refuses to use session variables. Is this a realistic goal and should session variables be avoided for any practical reasons? Can session variables be avoided completely (except for session cookies to allow logins) and would this result in better designs?
Some of the reasons my colleague has for not using them:
Untyped nature of session variables
Session time-outs causing loss of state
Global scope nature of session variables
Load balancing servers losing sessions (.Net specific?)
Application pools/servers restarting
They are unnecessary
Explanation / Answer
If you have a session variable in your application, ask yourself this:
When I click the back button of my browser, what value do I want my variable to have?
If the answer is "the current value", session variables may be useful. An example would be a shopping cart: you don't expect things to be removed from the shopping cart as you go back through the history. It's always in its current state.
If the answer is "a previous value", you should not be using session variables. Bad uses I have seen include passing a parameter between pages. If I click the back button to get back to a page, the page does not necessarily get the correct parameter. Also, if I open two tabs, how is my site going to behave then?
Getting the back button behaviour right is by no means the be-all-and-end-all, but it helps you think about a web site as a stateless application. In general, I find appropriate uses of session variables to be few and far between.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.