I have an example of some code that I often see on websites that I would like to improve, and would appreciate some help. Often I see 5-10 nested if statements in the page_load method, which are aimed at eliminating invalid user input, but it looks ugly, it is difficult to read and maintain.
How do you recommend clearing the following code sample? The main thing I'm trying to eliminate is the nested if statements.
string userid = Request.QueryString["userid"]; if (userid != ""){ user = new user(userid); if (user != null){ if (user.hasAccess){ //etc. } else{ denyAccess(INVALID_ACCESS); } } else{ denyAccess(INVALID_USER); } } else{ denyAccess(INVALID_PARAMETER); }
As you can see, it gets very dirty very fast! Are there any patterns or methods that I must follow in this case?
coding-style if-statement
Nickgps
source share