Is it a good practice to call a method that returns true or false values in an if statement?
Something like this:
private void VerifyAccount()
{
if (!ValidateCredentials(txtUser.Text, txtPassword.Text))
{
MessageBox.Show("Invalid user name or password");
}
}
private bool ValidateCredentials(string userName, string password)
{
string existingPassword = GetUserPassword(userName);
if (existingPassword == null)
return false;
var hasher = new Hasher { SaltSize = 16 };
bool passwordsMatch = hasher.CompareStringToHash(password, existingPassword);
return passwordsMatch;
}
or is it better to store them in a variable then compare them using if else values like this
bool validate = ValidateCredentials(txtUser.Text, txtPassword.Text);
if(validate == false){
//Do something
}
I am not only referring to .NET, I am referring to the question in all programming languages it just so happens that I used .NET as an example
if (!validate)
rather thanif (validate == false)
.IsValidCredentials
, although grammatically awkward, is a common format for indicating a boolean return value.!
is the "NOT" operator, it negates any boolean expression. Soif (!validate)
is the opposite ofif (validate)
. The if statement will be entered ifvalidate
is not true.