Thumb Rules for Good Variable Naming in Programming Language

Naushil Jain
3 min readDec 19, 2020
Thumb Rules for Good Variable Naming in Programing Language
Photo by Sigmund on Unsplash

In Programming Language choosing the right variable name is one of the important thumb rules. At the beginning of the programming, we do not consider it as a good practice. But When the code base is getting bigger with many more developers working on it, it just gets so much difficult to manage and coordinate our efforts. Our code is our creation, therefore, we ought to name it properly at the very least. So, by investing some time you will also realize that choosing the right variable name is very useful and increase our accuracy in programming. It will reduce lots of confusion if we properly define variable naming.

Tips For Good Variable Naming

  • Don’t Use Reserved Words: Do not use “reserved words” of any programming languages. A “reserved words” are the predefined words already used by that language.

For Example: “int”, “for”, “break”, “case”, “default”, “switch” etc.

  • Add Commenting: If possible always add a comment if that variable has used at multiple places. A comment will be used to identify the use of that variable and improve readability. Commenting is good practice in all the programming languages.
  • Don’t use too long variable names: Long names will bring ugly and hard to read code, also may not run on some compilers because of the character limit. So do not use more than 30(max) characters for variables.
  • Use Meaningful Name: Always use a meaningful name that explains the meaning of that variable.

For Example: counter, statusFlag, userList.

  • Use Camel Case: Always use the camel case while you are defining the variable. “Camel case” combines words by capitalizing all words following the first word and removing the space.

For Example: userList, employeeList, productList, employeeSalary, etc

  • Don’t use Clutter like “_” and “-”: Do not use “_” or “-” in variable naming. Always try to use camel case.

For Example: employee_salary, _emp etc

  • Don’t reuse the same variable name: Don’t reuse the same variable name in the same class in different contexts: e.g. in method, constructor, class. So you can provide more simplicity for understandability and maintainability.
  • Define Constant in Capital: Define constant always in capital letter. If your constant name contains more than a single work then use “_” to combine them.

For Example: “MAX_SALARY”, “TOTAL_PAGE”, “MAX_PAGE” etc.

  • Use database Field column name in HTML form: In the form where you are defining all your HTML, Try to use the same field name which you had used in the database tables. So it will reduce your field mapping before storing it in the database.
  • Manage Consistency: Obey programming language standards and don’t use lowercase/uppercase characters inconsistently.

For Example: userName, UserName, Username etc.

  • Make good use of common verb e.g. is, has, can or do: Naming boolean variables and methods with is, has, and can improve code readability. Methods like isEmployee(), hasAccount(), canExecute() adds lot of value. You can use the same rule to name boolean variable, which is easy to read when put on a conditional statement like

if(hasAccount){

}

Generally, Variable-length maybe 1 char for loop counters, 1 word for condition/loop variables, 1–2 words for methods, 2–3 words for classes, 3–4 words for globals. Different organizations have different variable naming standards. So always obey the organization’s naming standards and write variable names consistently in an application: e.g. txtUserName, lblUserName, cmbSchoolType, … Otherwise, readability will reduce and find/replace tools will be unusable.

--

--