Very simple question, but I want to start using a consistent naming convention for validation methods and I can't think of the best way!
Do people tend to use IsDataValid() style? or are there any others that are more descriptive and meaningful?
Cheers
It depends what your validation method does.
If it returns a Boolean, then probably starting with
Is
and ending withValid
is a good place to start. Usingis
for Boolean calls generally leads to readable code inif
statements.If your validation method throws an exception, then I'd usually start the method name with something like
Check
instead.However, also worth considering (as methods should usually use verbs) is beginning the method name with
Validate
. TheIs
style is generally more applicable to properties.As with anything involving naming conventions, there's no such thing as a right answer, but there's a lot of common problems with validation methods that lend themselves towards a certain approach, namely:
One approach I've found to be useful is to have a seperate validator class for each model object I want to validate that implements a common
IValidator
interface, usually with the following methods:This allows a pretty natural usage within your business logic:
I typically use the 'Is' MethodName style when the method returns a single Boolean value. It is perfectly acceptable in terms of naming. A lot of times data validation is done within the Set of a Property rather than a method so in this case you don't need to change the property name to indicate it validates the data set on it.
Here is a link that gives some general naming guidlines which you might find interesting as well:
Naming Guidelines: http://msdn.microsoft.com/en-us/library/xzf533w0(v=vs.71).aspx