Core JAVA

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
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago