Re: do-while vs. if..else if...if...else..if...else if.....



"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.






.



Relevant Pages

  • RE: Web Forms Auth fails when rfValidator triggered
    ... © 2002 Microsoft Corporation. ... | Content-Type: text/plain ... | | basically has a username field, ... | | If I enter garbage text in BOTH fields, the authentication ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • RE: Adding a virtual FTP folder to IIS
    ... I think we can follow the Form Authentication modal. ... application will use the ASPNET account. ... If we change the username ... Windows identity different from that of the default process identity. ...
    (microsoft.public.dotnet.framework)
  • Re: OWA login problems
    ... But anyway, since just using USERNAME works from the desktop, this indicates ... Maybe one of the authentication ... Outlook Web Access For PDA, ... the Virtual Directory named Exchange and select properties. ...
    (microsoft.public.exchange.connectivity)
  • RE: Web Forms Auth fails when rfValidator triggered
    ... | basically has a username field, ... | If I enter garbage text in BOTH fields, the authentication ... | controls do their job and display the "error text" stating ... | Jeff Ptak ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: RWW authentication
    ... By entering just the username without any ... Much of this RWW info is right inside of there. ... but the Isa firewall ... SSL authentication seems to work just fine however on the actual RWW login ...
    (microsoft.public.windows.server.sbs)