Tuesday, March 06, 2007

How to Validate Arguments Of Public Methods? (Microsoft Design - CA1062)

FxCop can drive you crazy, but ... It improves the quality of your code!
Tip: enable the Code Analysis in the beginning!

I am sure that you have ever seen the following FxCop warning:
Microsoft.Design --> CA1062:ValidateArgumentsOfPublicMethods

This article gives you a uniform way of validating parameters!


1. Validating parameters in every public method
If you have to check every parameter every public method. This means that you add at least 3 lines ...

Example
if( condition )
{
// exception statement;
}

Disadvantages:

  • Re-writing code every time you need to check parameters
  • 3 lines of code extra in every public method which has parameters.

2. Using the ParameterCheck class for Parameter validations
You can reduce the 3 lines of code into 1 line. Just make a separate Visual Studio Project which is responsable to check parameters. It is a static class which has static methods.

ParameterCheck Method Overview

  • NotNullOrEmpty
    Checks if a string is empty or null
  • NotNull
    Checks when an object is null. In case of a DateTime object, it cannot be the MinValue (which will be compared with null)
  • WithinRange (for int)
    Checks if the Int value (16/32/64 bit) has a value within the range (ranges inclusive)
  • WithinRange (for double)
    Checks if the decimal value (decimal, single, float, double) has a value within the range (ranges inclusive)
  • WithinRange (for DateTime)
    Checks if the DateTime value has a value within the range (ranges inclusive)
  • BiggerOrEquals (for int)
    Checks if a number has a value which is bigger or equals to the minimum value (minimum value included)
  • SmallerOrEquals (for int)
    Checks if a number has a value which is smaller or equals to the maximum value (maximum value included)

Example
public static void NotNullOrEmpty(string parameterName, string parameterValue)
{
if (String.IsNullOrEmpty(parameterValue))
{
throw new ArgumentNullException(parameterName);
}
}


Advantages

  • Just 1 line of code!
  • Reusability!
  • Same result
Disadvantage

  • FxCop/Code Analysis does not recognize your class as being a valid parameter validation. You should add a SuppressMessage (right-click on the warning in the error list and select 'suppress message')

You can download the code of this article here.

2 comments:

Geronimo said...

Stoffel, is die ParameterCheck class opgebouwd uit bestaande functies? string.IsNullOrEmpty bestaat al, dus die NotNullOrEmpty is daaruit verder gebouwd ofwa?
Van die ander hebbek geen idee

Kristof De Causemaeker said...

Hello Geronimo!

The class in this post is a "home made class" which not only checks but throws an ArgumentException/ArgumentNullException when a condition is not met ...

For example the NotNullOrEmpty method uses the existing String.IsNullOrEmpty method but ... the check is included in the method body and throws an error when it is empty or null.

Normally you would check every time you have a string, when it is empty or null. Now you must only add 1 line per parameter to check its value.

ParameterCheck.NotNullOrEmpty("name of variable", variableValue)

I will write some code in the following post.