Be careful with sample code
Found this code somewhere, it doesn't matter where:
The comments are there because it's a sample, not because they should be there in production code, so that's fine. My problem is with the boolReturn variable.
First of all, one rule (Code Complete should really be required reading for programmers) is that you want to keep assignments to a variable as close as possible to the place where the variable is used. The oUserProvider and oUser variables have nothing to do with the boolReturn, so let's switch the order:
Ok... is the boolReturn variable used anywhere after the return? Not before it's reassigned. That means that, for all intents and purposes, the scope of the variable boolReturn ends here, and a *new* boolReturn variable is created. (Think about it.)
Let's rewrite the code accordingly (think it through to verify that nothing was changed in the meaning of the code):
Progress. This change makes visible another problem: why set a variable if you're only using it once? (Ok, it makes sense for complicated calculations, but it's not the case here.)
Frankly, I find the ?: operator to be a great help, so my code would tend to look like the next example, but I understand some people get confused by it, so take your pick:
Finally, what's up with the (bad) Hungarian notation? The fact that the name (or email, whatever) is a string or something else is relevant for the compiler, not for the programmer. The good Hungarian notation can be useful, but most people seem to only know about the bad variant. Let's get rid of it altogether:
To me, this looks much better than the original. Doesn't it?
- public override bool ValidateUser(string strName, string strPassword)
- {
- //This is the custom function you need to write. It can do anything you want.
- //The code below is just one example.
- // strName is really an email address in this implementation
- bool boolReturn = false;
- // Here is your custom user object connecting to your custom membership database
- MyUserProvider oUserProvider = new MyUserProvider();
- MyUser oUser = oUserProvider.FindUser(strName);
- if (oUser == null)
- return boolReturn;
- // Here is your custom validation of the user and password
- boolReturn = oUser.ValidateUsersPasswordForLogon(strPassword);
- return boolReturn;
- }
The comments are there because it's a sample, not because they should be there in production code, so that's fine. My problem is with the boolReturn variable.
- bool boolReturn = false;
- // Here is your custom user object connecting to your custom membership database
- MyUserProvider oUserProvider = new MyUserProvider();
- MyUser oUser = oUserProvider.FindUser(strName);
- if (oUser == null)
- return boolReturn;
First of all, one rule (Code Complete should really be required reading for programmers) is that you want to keep assignments to a variable as close as possible to the place where the variable is used. The oUserProvider and oUser variables have nothing to do with the boolReturn, so let's switch the order:
- // Here is your custom user object connecting to your custom membership database
- MyUserProvider oUserProvider = new MyUserProvider();
- MyUser oUser = oUserProvider.FindUser(strName);
- bool boolReturn = false;
- if (oUser == null)
- return boolReturn;
Ok... is the boolReturn variable used anywhere after the return? Not before it's reassigned. That means that, for all intents and purposes, the scope of the variable boolReturn ends here, and a *new* boolReturn variable is created. (Think about it.)
Let's rewrite the code accordingly (think it through to verify that nothing was changed in the meaning of the code):
- public override bool ValidateUser(string strName, string strPassword)
- {
- //This is the custom function you need to write. It can do anything you want.
- //The code below is just one example.
- // strName is really an email address in this implementation
- // Here is your custom user object connecting to your custom membership database
- MyUserProvider oUserProvider = new MyUserProvider();
- MyUser oUser = oUserProvider.FindUser(strName);
- bool boolReturn = false;
- if (oUser == null)
- return boolReturn;
- // Here is your custom validation of the user and password
- boolReturn2 = oUser.ValidateUsersPasswordForLogon(strPassword);
- return boolReturn2;
- }
Progress. This change makes visible another problem: why set a variable if you're only using it once? (Ok, it makes sense for complicated calculations, but it's not the case here.)
- public override bool ValidateUser(string strName, string strPassword)
- {
- //This is the custom function you need to write. It can do anything you want.
- //The code below is just one example.
- // strName is really an email address in this implementation
- // Here is your custom user object connecting to your custom membership database
- MyUserProvider oUserProvider = new MyUserProvider();
- MyUser oUser = oUserProvider.FindUser(strName);
- if (oUser == null)
- return false;
- // Here is your custom validation of the user and password
- return oUser.ValidateUsersPasswordForLogon(strPassword);
- }
Frankly, I find the ?: operator to be a great help, so my code would tend to look like the next example, but I understand some people get confused by it, so take your pick:
- public override bool ValidateUser(string strName, string strPassword)
- {
- //This is the custom function you need to write. It can do anything you want.
- //The code below is just one example.
- // strName is really an email address in this implementation
- // Here is your custom user object connecting to your custom membership database
- MyUserProvider oUserProvider = new MyUserProvider();
- MyUser oUser = oUserProvider.FindUser(strName);
- return oUser == null
- ? false
- : oUser.ValidateUsersPasswordForLogon(strPassword);
- }
Finally, what's up with the (bad) Hungarian notation? The fact that the name (or email, whatever) is a string or something else is relevant for the compiler, not for the programmer. The good Hungarian notation can be useful, but most people seem to only know about the bad variant. Let's get rid of it altogether:
- public override bool ValidateUser(string name, string password)
- {
- var userProvider = new MyUserProvider();
- var user = userProvider.FindUser(name);
- return user == null
- ? false
- : user.ValidateUsersPasswordForLogon(password);
- }
To me, this looks much better than the original. Doesn't it?
Comments