I know some people will say it's a based opinion question but I really don't think so, since it's a convention already but I'm missing something.
I'm trying to understand better the Clean Code way of coding and sometimes I just get confused about the right approach.
I'll use as example, the Auto Login with AD on a system. The rule says: A user can LogIn on system using the AD account. If he is not "allowed" yet to use the system, we have to check a parameter on the settings. If the parameter says AutoCreation TRUE, we create a new credential and allow him to login.
Ok, said that, here comes the code:
// WE HAVE CODE HERE
var userInfo = GetUserInfo(request);
if (userInfo == null)
{
var isAutoUserCreationEnabled = GetAutoUserCreationStatus();
if (isAutoUserCreationEnabled)
userInfo = AutoCreateUser(request);
}
//AND WE HAVE MORE CODE HERE
The method GetUserInfo has just a call to the Data Layer and returned the found user. If the user is NULL, we now call the other method to check the GetAutoUserCreationStatus, which identifies if the AutoCreate is true or false and if true, create the user on database.
It's looks ok, but since my method (the main method) has more rules, the function with 5 lines is impossible. So, I was thinking if the correct way isn't put on the GetUserInfo method all the details to get the user. So the main method will be:
// WE HAVE CODE HERE
var userInfo = GetUserInfo(request);
//AND WE HAVE MORE CODE HERE
And the
if (userInfo == null)
{
var isAutoUserCreationEnabled = GetAutoUserCreationStatus();
if (isAutoUserCreationEnabled)
userInfo = AutoCreateUser(request);
}
goes inside the GetUserInfo. But I feel like the GetUserInfo(AndCreateUser) method now has 2 responsabilities and it's wrong.
So now I'm confused about how it really should work. What is the right or better way to be a clean coder?
Thanks in advance guys.
Aucun commentaire:
Enregistrer un commentaire