Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this bad code?

Tags:

c#

.net

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?

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

like image 846
Xaisoft Avatar asked Nov 27 '22 12:11

Xaisoft


2 Answers

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

like image 184
Robban Avatar answered Dec 24 '22 06:12

Robban


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.

like image 28
Michael Gattuso Avatar answered Dec 24 '22 06:12

Michael Gattuso