Best Practices in Programming to Decide Name of Variables, Methods, Classes and Packages

Naming of Variables, Methods, Classes and Packages is very important to decide proper and understandable. When we learn Core Java Naming convention is very first chapter in java basics. Actually proper naming conventions make programs more understandable by making them easier to read. Naming conventions is not only for Variables but also for the methods, constants, package and class which can be helpful in understanding the code.

Follow Java Naming Convention

According to standard Java naming conventions, you make your code easier to read for yourself and for other programmers. There are following points should be follow by each developers.

  • class name->should start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc.
  • interface name->should start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc.
  • method name->should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc.
  • variable name->should start with lowercase letter e.g. firstName, orderNumber etc.
  • package name->should be in lowercase letter e.g. java, lang, sql, util etc.
  • constants name->should be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.

There are following points of best practices in programming to decide name of Variables, Methods, Classes and Packages.

Avoid Meaningless Names of Variables

Meaningless Names of Variables should be avoid like name as xyz, a, aa, bbb, i, iii, temp etc. Meaningless names doesn’t reveal intent, they simply reduce readability. You should never use pointless or meaningless name even you are making Hello Java project because it makes your habit. In a real project it would never acceptable by code reviewer because hello world programs are not maintained but a real project is maintained for years.

Always use Meaningful Names of Variables

This point is counterpart of first point. According to best practice of naming conventions inn java you should always use meaningful name instead of pointless names. For example variable name index is much better than like i,ii,j etc and method name with getJoinDate() is also much better than getJD(), jDate(), jdt() because method name getJoinDate() understandable name in future for any other developers in somewhere in my code.

Shorter name preferable over longer name

Shorter name is always preferable over longer name if it reveal intent clearly. For example between getJoinDate() and getEmpSalary() both are able to reveal purpose, shorter ones are easy to read and write, but don’t forget to follow Java bean naming convention e.g. if variable name is joinDate then getter method name must be getJoinDate().

Prefer descriptive name over short form

This point is counterpart of above point. In programming intent revealing is also important point to decide name of variables, methods etc. For example getJoinDate() is fine but getESal() is not as good as getEmployeeSalary() or getEmpSalary(). So prefer shorter name if and only if it reveal intent completely, otherwise choose longer and descriptive name.

Avoid Similar Names

Never use similar names like employee and employees, both variable have every same character except last one. This kind of variable names might be reason of very hard to spot and subtle bugs and also these names are even harder to find during code reviews. For single employee use employee or emp and for list employees always use like listOfEmp or listOfEmployees over employees.

Consistency in Naming of Methods

According to best practice of programming, you should always use consistent naming convention in a project. Suppose for a project you are using close(), finish(), kill() etc. for releasing the resources, these names are not in consistent so we always consist with one name of them for resource release. This will make your API more usable, as programmer will be able to predict and search more easily.

Class, Interface and Method Name in Java

Class name should be noun, Interface name should describe ability and method names should start with verb. For example class names should be Employee, Student, Manager, Method names should start with verb e.g. get, set, do, invoke etc and Interface name should Cloneable can clone, Runnable can run, Callable can be called etc. And package name should follow standard company structure e.g. com.company.project.module.

Classical Programming Convention for loop
for(int i=0; i<10; i++){
// your code
}
In such case we can pointless name for for, while loop as above.

Avoid name like _, d_, s_

Avoid using non ASCII characters

Never use characters from local languages like Hindi, German etc. English is a universal language for programming and stick with it.

Boolean Variable and Method Names
For Boolean varable always use name like isActive, isDeleted etc. Methods name should be like isAlive(), hasNext(), canExecute() adds lot of value.

Previous
Next