1
Vote

Improve coverage of input validation

description

We need to improve coverage of input validation for public input. Any method, constructor, property, indexer, etc that is public needs to have proper input validation to ensure that no part of the code can ever enter an invalid state.
 
Also, where appropriate, "unchecked" methods should be implemented, marked as internal, to be used for speed when the input is known to already be valid (it has already been checked, or is provided as a constant, for example).
 
Example:
 
internal Size GetRenderedSizeUnchecked(string text)
    {
        return new Size(text.Length * this.CharacterSize.Width, this.CharacterSize.Height);
    }
 
public Size GetRenderedSize(string text)
    {
        if (text == null)
        {
            throw new ArgumentNullException("text");
        }
        else if (text.Length == 0)
        {
            throw new ArgumentException("Argument 'text' cannot be empty.");
        }
        return this.GetRenderedSizeUnchecked(text);
    }

comments

WShelley wrote Apr 13, 2008 at 5:54 AM

Instead of "SomethingUnchecked" as the method name, it should be "SomethingUC", this looks cleaner IMHO and is consistant with what already exists.

Also, properties that need validation (in the setter for example) should not be implemented with an internal unchecked version.

wrote Feb 13, 2013 at 3:45 AM