Java Coding Standards and Conventions

java coding standardsEvery programming language has some de-facto conventions and guidelines that must be kept in mind while writing programs in that particular language. These guidelines and standards are evolved by examining the efficiency of the application, through rigorous experimentation and research. Like all other advanced programming languages, Java also defines a set of standards and guidelines that a developer should follow. These guidelines are provided by the Oracle Corporation (the developers of Java). Following these standard guidelines results in a more readable code that can be easily maintained in the future and can also be used to communicate the code features between a team of developers.

Interested in learning how to code Java? Take a course at Udemy.com.

Naming Classes

In Java, the name of the class should be purely a noun. A class basically represents an entity in a software application. For instance, in a car racing computer game, a Car can be Class. Most appropriately, a class name should not consist of more than one word. For instance, it is always better to name a class Car rather than DrivingCar.

Another extremely important issue to note is that a class name in Java should be written in Pascal case. In Pascal case, first letter of every word should be a capital. Therefore, for the previous car race game example, the class name should be Car and DrivingCar rather than car and drivingcar respectively.

Naming Interfaces

Interface names should ideally be adjectives and should end in the prefix “able”. For example, if in a car race game, a car can be converted into some other vehicle type such as a motor bike or truck. The Car class should implement an interface named Convertible, which would contain methods that convert the Car class instance to other vehicle type instances. Convertible is an adjective in this case which refers to the ability of the Car class to be converted into some other vehicle type. By convention, the first letter of every interface should be capital.

Naming Methods

In Java, methods refer to some functionality performed by the object of the class that contains that method. Therefore, logically thinking in this context, a method is ideally named as verb. A method should be readable enough so that it conveys the functionality that it actually encapsulates. For instance, in a car-race game where a Car class is used to manage the Car in the application, a start or startCar method can be used to encapsulate the car start functionality. Both start and StartCar are basically verbs. It is advisable to use single name for methods, as is the case with class names.

An important convention that needs to be kept in mind is that method names in java are written in camel case i.e the first letter of the first word is in lower case and the first letter of the remaining words in method names are capital. For instance method name that starts the car should be start or startCar rather than Start or StartCar respectively.

It is never advisable to package multiple functionalities into one method. If a method performs many functions, it should be broken down into multiple methods each performing a specific functionality.

Naming Attributes/ Member Variables

Attributes and member variables should be named singular nouns. For instance, extending the car race game example, a Car class can have several attributes such as number, name, model, etc. All of these variables have been named as nouns.

Like Java methods, the attribute names should also follow, camel case naming convention where the first letter of the first word in the attribute name should be small followed by the capitalized first letter of all the remaining words. For instance, carNumber is a valid naming convention rather than carnumber of car_number.

Naming Constants

Every class often contains some constant values that are not changed throughout the life-cycle of the instance of the class. Such values are called constants. For instance, the Car class can contain an integer type constant value of maximum speed which can be any constant. Conventionally, these constant variables should be named as:

public final static int Max_Speed = 1000;

Constant values can be declared public unlike variables since they are required to be accessed in multiple instances of the class. Declaring a variable final in java means that the value of that variable cannot be changed.

The first letter of every word in a constant variable’s name should be capitalized. If a constant variable contains multiple names, they should be separated by underscores.

For more interesting Java tutorials, see the courses Udemy.com has to offer.

Using Getter & Setters

In Java, variable members or the attributes of class must never be publicly exposed. Rather, accessors (also known as getters) and mutators (also known as setters) should be used. Accessors and mutators are basically public functions that are used to get and set the values of the attributes of a class. For instance, if a class has an attribute name, following accessors and mutators methods can be used:

Accessor:

public String getName()
{
return name;
}
Mutators:
public void setName(String name)
{
this.name = name;
}

Accessors and mutators are used to force data encapsulation which is one of the three basic pillars of object oriented programming with polymorphism and inheritance being the other two.

Import Packages Rather than fully qualified names

Not only in Java but in every programming language, it is a good practice to import namespaces or packages in the file rather than repeatedly writing fully qualified names to access the types in those classes. For instance to use Data object in a Java program, it is always better to first import java.util.Date package to a program and then directly instantiating Date class in the program, rather than writing fully qualified name again and again.

To learn more about Java, take a course at Udemy.com.