Having such an ability will let us remove validation code from the method body and move it to the parameter/argument level, therefore, fixing responsibility of sending validated variables to the calling method.
If the calling method passes a variable that does not support the validation rule, an invalid argument exception is thrown!
I propose a type called ValidVariable or ValidateableVar
examples:
//ensure that only 2 digit numbers are added
public int Add2DigitNumbers(ValidVariable(10,99) first,ValidVariable(10,99) second)
{
...
}
//ensure only valid emails are supported
public void SetEmail(ValidVariable(regex_for_email))
{
...
}
If only the next version of c# supported it! or maybe I will start a project on CodePlex.
No comments:
Post a Comment