Reputation: 14765
If you validate something in code you can either work with a return value indicating that something is wrong or you can throw an exception. In my controller I have Exceptional validation like this:
void DoSomething()
{
Validate(); // throws exception if something is wrong
.....
}
I wonder if there is a common naming convention that implies that an exception is thrown when something is wrong so that I don't need to add the comment // throws exception if something is wrong
and distinguishes from if (!IsValid())
Note: validation-naming-conventions does not answer my question.
Update after accepting the answer: What I have learned from this Question
if (IsValid(...))
does not miss something (tnx @ Cody Gray)Upvotes: 1
Views: 877
Reputation: 5540
Typically you would use:
// for a validations that returns just plain yes no (true|false). in the case of a property use caching for the last validationresult.
bool IsValid
// for a validation that returns a list of errors of some sort (messagelist, id list, custom objects, whatever you need).
object Validate();
// validates and throws an exception if one or more error occured.
void ValidateAndThrow();
Also make sure to consider if you need warnings of some kind. For example if you validate your registration DTO model and want to warn a user if the password is weak but do not want to prevent the user from saving it if he chooses to.
Upvotes: 2