Thursday, June 17, 2010

Hardcoding values

Do not hardcode any values (numbers and string used with in application). Use a separate file for declaring and defining the constant values either at the assembly (module) level or at the application level.

C# example- In the below example constants class is used for storing all the constants.
class Constants
{
    public const int MinEmpId = 0;
}
//employee class
class Employee
{
    public string Name { get; set; }
    public int EmpId { get; }
    public Employee()
    {
    }
    //should not allow negative numbers
    public bool SetEmpId(int iEmpId)
    {
        bool blnStatus = false;
        if (iEmpId >= Constants.MinEmpId)
        {
            EmpId = iEmpId;
            blnStatus = true;
        }
        return blnStatus;
    }
}

Note: Follow the internationalization standards if your application has to support localization.

Rule: Do not hardcode values.

Rule: Follow internationalization standards if required.

No comments: