Wednesday, August 18, 2010

Naming convention

There are no hard and fast rules as far as naming conventions are concerned, but it has to be consistent at the individual, team and organization level.

The name of the folder, project, file, namespace, class, and function have to be meaningful. The variable (data members, local members or function arguments) must have the following information as part of their name
  • modifier (private, public, etc.) and scope of the variable (member to the class (data member), member to the function (local variable), constant, static)
  • datatype of the variable
  • meaningful name
Along with the above mentioned points you can also add comments where ever necessary. Also indentation of the code would improve the readability.

C# 2.0 example- Below is the Employee.cs file.
using System;
using System.Collections.Generic;
using System.Text;
namespace Sample_v2
{
    class Employee
    {
        private string m_strName=String.Empty;
        private int m_iEmpId=-1;
        public Employee(string strName, int iEmpId)
        {
            m_strName = strName;
            m_iEmpId = iEmpId;
        }
        public string Name
        {
            get
            {
                return m_strName;
            }
            private set { m_strName = value; }
        }
        public int EmpId
        {
            get
            {
                return m_iEmpId;
            }
            private set { m_iEmpId = value; }

        }
        //should not allow empty string or null string
        public bool SetNameWithValidation(string strName)
        {
            bool blnStatus = false;
            if (strName != string.Empty && strName != null)
            {
                m_strName = strName;
                blnStatus = true;
            }
            return blnStatus;
        }
        public override string ToString()
        {
            return string.Format("Name:{0},EmpId:{1}",
                m_strName, m_iEmpId);
        }

    }

}

C# 3.0 example- Below is the Employee.cs file.
using System;
using System.Collections.Generic;
using System.Text;
namespace Sample_v3
{
    class Employee
    {
        public string Name { get; set; }
        public int EmpId { get; set; }
        public Employee()
        {

        }
        //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;
        }
        public override string ToString()
        {
            return string.Format("Name:{0},EmpId:{1}",
                Name,
                EmpId);
        }
    }
}

Note: Alternatively you can follow the naming standard followed by the respective technology (.Net, J2SE, JavasScript, etc). Intellisense feature of the respective integrated development environment (IDE) could help you on this.

Rule: Follow consistent naming convention.

Rule: If you not clear on the final name of the product, module, etc consider using a meaning less code name (e.g. "code123").

No comments: