I’m working on a project where exist one method with title
string ValidateNewPasswordExpireCurrentPasswordAndCreateNewPassword(...)
I’m sure the method name must be changed. But can’t found good and descriptive alternative.
Also I can’t separate method in multiple methods, like ValidateNewPassword(), ExpireCurrentPassword() and CreateNewPassword().
How can I rename the method and the same time to not lose self describing ability ?
10
I expect this method to call three other methods:
public string ValidateNewPasswordExpireCurrentPasswordAndCreateNewPassword(string password)
{
AccountManager.ValidatePassword(password);
AccountManager.ExpirePasswordForAccount(this.CurrentAccount);
AccountManager.AssignPasswordToAccount(this.CurrentAccount, password);
}
When a method aggregates different things, it’s done because:
-
Either it’s too cumbersome to call those different things in order every time you need them, i.e. the caller don’t want to remember that the password should be validated, then the old one should be expired and only then the new one should be assigned.
The caller probably needs to know what’s happening under the hood, since the same caller can call those three methods separately. You can try to shorten the method name, but not too much: sadly, you still need to be very specific and detailed.
You can also move the hint that the password is validated to the password argument itself.
public string ExpireAndReplacePassword(string passwordToValidate) { AccountManager.ValidatePassword(passwordToValidate); AccountManager.ExpirePasswordForAccount(this.CurrentAccount); AccountManager.AssignPasswordToAccount(this.CurrentAccount, passwordToValidate); }
-
Or you don’t trust the caller: what if the caller just assigns an invalid password?
In this case, the caller doesn’t have to know the internals, so be short. You don’t need to specify that there is input validation: you’re expected to validate inputs from an unstrusted caller.
public void ReplacePassword(string newPassword) { Contract.Requires(AccountManager.IsPasswordValid(newPassword)); AccountManager.ExpirePasswordForAccount(this.CurrentAccount); AccountManager.AssignPasswordToAccount(this.CurrentAccount, newPassword); }
or, if there is still a reason to tell that the old password will expire:
public string ExpireAndReplacePassword(string newPassword) { Contract.Requires(AccountManager.IsPasswordValid(newPassword)); AccountManager.ExpirePasswordForAccount(this.CurrentAccount); AccountManager.AssignPasswordToAccount(this.CurrentAccount, newPassword); }
-
Or business-wise, separately, those actions don’t make sense.
In the third case, if only the aggregation makes sense business-wise, you can simply use the verb which is used in this domain. For example, if the users don’t expire their passwords, nor validate them, nor assign them, but simply change them, the change implying the validation, the expiration and the assignment, call the method simply:
public string ChangePassword(string password) { Contract.Requires(AccountManager.IsPasswordValid(password)); AccountManager.ExpirePasswordForAccount(this.CurrentAccount); AccountManager.AssignPasswordToAccount(this.CurrentAccount, password); }
string ChangePassword(...)
{
if(ValidateNewPassword(...))
{
ExpireCurrentPassword();
CreateNewPassword();
return ... //Method signature is string?
}
else
{
//Handle invalid password
}
}
Much cleaner IMO!
As mentioned by others I would go with UpdatePassword
or RenewPassword
as both imply that a password mus exist and that actions are being carried out on it which may or may not change it. The best approach for me personally in such situations has been to rename the function then wrap the function in the old name as well, leaving legacy compatibility in place for a while, and having a log message outputting the new name of the modernised function. That way you can allow programmers time to get accustomed to the new Name without breaking the codebase, and coincidentally saving you having to make plenty of refactoring code changes yourself.
2