tags:

views:

334

answers:

6

I was looking at a question on here about confessing your worst code ever written and I am not quite sure, because of my lack of knowledge on why this is bad code.

public string GetUsername (string userName)
{
 User user = DbLookup.GetUser(userName);
 return user.Username;
}

Is it because, it assumes username will exist and doesn't check for null? Or is there more to it?

http://stackoverflow.com/questions/130965/what-is-the-worst-code-youve-ever-written/191969#191969

+16  A: 

because it returns the same thing that the user sends as input to the method... Username

Robban
well duh, now i fell more stupid, lol. Thanks.
Xaisoft
I can imagine a scenario where the username passed in is somehow different from the one pulled from the database. But the function should be named in way to make that clear if that's the case.
Mark Biek
Depends. If the `userName` passed into `GetUser()` may be an alias, or if you want to get the capitalization correct, this may not be so bad at all.
Lucero
It doesn't return the same thing - it can return null or an exception.
Michael Gattuso
You're all correct of course, there's no way to be sure that what is returned is actually the same as what was inputted. However it obviously looks like it is getting the user by the username and then returning the username which is why it's bad code.
Robban
Depending on if the username passed into it exists in the database and is the same username in the database, it will return what is passed into it, but if the database happened to have two columns for usernames, one could be used for the passed in value and one could be used for the retreived value. Is this correct?
Xaisoft
@Xaisoft: You could be correct because the `DbLookup.GetUser(..)` method implementation is not provided to us. Therefore anything you can imagine inside of it could be correct.
John K
+2  A: 

Because he returns the same string he passed into the method.

bleeeah
Thanks. It makes more sense one someone points it out. I was looking at it from checking if a user exists in a db.
Xaisoft
This answer is wrong! The method actually looks up a User based on the string, and then returns the User's Username property, which may or may not be the same string.
Tom Bushell
+1  A: 

because if user exists, it just returns the same value submitted as method parameter, and if user does not exist, it will throw a null reference exception.

Charles Bretana
true, but it very well could return a different username than the one passed into it, correct?
Xaisoft
+5  A: 

It doesn't return the same string that was provided. It return the username from the database and the user may or may not exist - thus it could return null. The method name is perhaps incorrect given what it does. Someone in the original post mentioned that it should be CheckIfUsernameExistsAndReturn sorta method name.

Michael Gattuso
Yes, I believe you are correct. I believe the code is not that bad, but it should have been named something else.
Xaisoft
+3  A: 

Besides the given answers, the method is actually named confusingly, now the maintainer has to dig around to figure out what it does.

leppie
lol, you probably remember the post because you made a comment on it.
Xaisoft
I did see that, but the second comment was more valid :)
leppie
+1  A: 

As already stated, it is bad code simply due to the fact the method is redundant really. It is returning (assuming User.Username is the same as the parameter) the same value of the parameter. However, as you mentioned another reason it is bad is because it doesn't check that User is null before it attempts to return the Username.

Another potential issue is GetUser may raise an exception that is not being handled in the method (it could indeed be getting handled externally or internally). Just a thought tho...

An improvement would be to return the User object rather than the username itself:

public User GetUser(string username)
{
     try
     {
          return DBLookup.GetUser(username);
     }
     catch (DBLookupException ex)
     {
          // throw exception or handle exception
          return null;
     }
}
James