Re: do-while vs. if..else if...if...else..if...else if.....
- From: "Leslie Sanford" <jabberdabber@xxxxxxxxxxxxxxxxx>
- Date: Sun, 1 Apr 2007 22:39:03 -0500
"Tarkin" wrote:
<snip>
Here's the actual logic: I have a log-in page for a
web application. The page forwards it's parameters
to a filter, which in turn calls the object I've written
to do the actual authentication. The object is called
the UserManager. The UserManager keeps track of
all users and their authentication status. So basically,
the filter sends a username and password to the
UserManager which then authenticates the supplied
parameters against it's records, modifies the user's
record accordingly, and sends a boolean back to the
filter. The UserManager also modifies a static field
which contains a message pertaining to the last
operation performed; the page uses this message
to indicate the nature of failure- the user is already
logged in, username doesn't exist, etc.
There are a number of possibilities to consider:
-the username or password are null
-the username does not exist
-the password does not match the record
-the user is already logged in
Which isn't all that bad- the Java source was
a little messy. The problem came when I wanted
to update the code to add new features- a group,
a group password, and roles. These two features
put a twist on things- for example, neither the
'superuser' nor 'admin' role should be able to login
with a group password - they *must* use their
own passwords.
IMHO, the strength of do...while(FALSE) method
lie in:
<snip>
-adding new checks simply involves inserting the
the appropriate if(condition) { ... } at the
appropriate location in the progressive logic
flow, i.e. you only need to check the role after
the username is valid, but before you check the
group password. Without the nice, neat if... blocks,
rearranging the if...else...if...else..if can become
problematic, in addition to the problem of sight
reading deeply nested 'if's and 'else's.
Ok, thanks for the explanation; it helps.
I'm going to sidestep the do/while issue and address this with a
different approach. I think the logic can be factored out into several
methods. This will help maintenance and readability, in my opinion. For
example, we could factor out dealing with roles in a separate function:
// In the main method...
if(username is null)
{
return "username is null" error;
}
else if(password is null)
{
return "password is null" error;
}
else
{
return LogIn(username, password, role);
}
ErrorMessage LogIn(string username, string password, string role)
{
if(role is superuser)
{
return LogInSuperUser(username, password);
}
else if(role is admin)
{
return LogInAdmin(username, password);
}
else
{
return LogInUser(username, password);
}
}
ErrorMessage LogInSuperUser...
ErrorMessage LogInAdmin...
ErrorMessage LogInUser(username, password)
{
if(DoesUserNameExists(username))
{
if(IsUserPasswordValid(username, password))
{
// ...
}
else
{
return "Invalid password.";
}
}
else
{
return "Username does not exist.";
}
}
Anyway, I think you get the idea. I'm shooting in the dark with my
example in that I don't know the details of your program. But hopefully
this may give you some ideas on how to factor out your logic into
methods.
.
- References:
- do-while vs. if..else if...if...else..if...else if.....
- From: Tarkin
- Re: do-while vs. if..else if...if...else..if...else if.....
- From: Leslie Sanford
- Re: do-while vs. if..else if...if...else..if...else if.....
- From: Tarkin
- do-while vs. if..else if...if...else..if...else if.....
- Prev by Date: Re: do-while vs. if..else if...if...else..if...else if.....
- Next by Date: Re: bison and valgrind
- Previous by thread: Re: do-while vs. if..else if...if...else..if...else if.....
- Next by thread: Re: do-while vs. if..else if...if...else..if...else if.....
- Index(es):
Relevant Pages
|