Because both IsValid()
methods do nothing else they are equivalent.
All 3 are correct. The third is my preference because it's less code and still very readable in this case.
Question
Which one is correct and WHY
in both examples we have a function that determines if a certain string is valid...
(using some other function that's not defined here)
private Validator = new Validator();
public Boolean IsValid(String foo)
{
if (Validator.Validate(foo))
{
return true;
}
else
{
return false;
}
}
in the second scenario we have a function that ends with a TRUE statement and with no else.
private Validator = new Validator();
public Boolean IsValid(String foo)
{
if (!Validator.Validate(foo))
{
return false;
}
return true;
}
NOW INB4 please dont say that you can simply do it this way
return Validator.Validate(foo);
How to save a few lines its not what i want to know...but the implications and unknown consecuences ( to me ) of using one method or the other.
Solution
Because both IsValid()
methods do nothing else they are equivalent.
All 3 are correct. The third is my preference because it's less code and still very readable in this case.
OTHER TIPS
I think that the best solution is:
public bool IsValid(String foo)
{
return (Validator.Validate(foo))? true : false;
}
In addition the conditional expression is easy to understand and it's inline