Thursday, June 17, 2010

Return variable

Initialize the failure status value to return variable. So that even if the success value is not set to the return value, the call would always fail and the developer can debug and fix the issue during unit testing or development.

C# example- In the below example blnStatus is initialized to false.
//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;
}

Rule: Initialize the return variable to failure status value.

Rule: Make sure return variable is set to success status value at the right place.

No comments: