Thursday, June 17, 2010

Argument validation

Validate the function arguments for the following
1) null or empty value
2) invalid datatype
3) out of range if applicable

Alternatively you can validate the arguments in the calling function and have called function perform only the business logic.

C# example
//should not allow empty string or null string
public bool SetName(string strName)
{
    bool blnStatus = false;
    if (strName != string.Empty && strName != null)
    {
        Name = strName;
        blnStatus = true;
    }
    return blnStatus;
}

Note: The intranet applications can handle the argument validations before calling the function in the server so that the server can focus only on executing the business logic.
Due to security reasons the internet applications should authenticate the caller, authorize the caller and validate the function arguments.

Rule: Validate the arguments for null or empty values.

Rule: Validate the arguments for wrong datatype.

Rule: Validate the arguments for out of range.

No comments: