Java Coding Standards Part 1

For the sake of every programmer and developer there are some very meaningful standards developed. Programmer should be aware of these coding standards as they are highly required by the industries. Like other programming languages, java standards not only have formatting style but they also have naming conventions.
To understand the need of coding standards analyze the following program written without java standards:

class X                       
{
public void p(String s)
{
System.out.println(s);
}
}

In the above code you can see the following mistakes:

  • Class name does not indicates its purpose just like other default classes in Java. Like String, Math, Exception etc.
  • Class should be made public in order to access it from other packages.
  • Since method doesn’t requires any global or instance variable it should be made static.
  • Name of method does not indicates its usage.
  • Variable s should be named more recognizable.
  • There should be comments to increase the readability of the code.

Considering the above mistakes, modifying the code according to the conventions.

public class PrintString                      //class to print the string
{ 
public static void print(String str)   //method to print the string, static method to call it without creating object
{ 
System.out.println(str);               //printing the String str in a new line
} 
}

Coding standards can be categorized in multiple sections to understand them more.


Coding Standards for Classes

Class names should reflect the motive they are made for. They should usually be nouns and should start with uppercase letter only. If it contains more than one words then they should not be separated by a space and every first letter of all words should be an uppercase letter. For example, consider the names of classes as

a                   //invalid, should be uppercase
A                   //invalid, should be noun
Student             //valid
Studentdata         //invalid, second word's first letter is not uppercase
StudentData         //valid

Note- Though the name of the classes should be noun, there is one such class which is adjective by its name. It is the only exception in the default packages. Name of the class is Throwable, it the base class of Exception and Error classes.


Coding Standards for Interfaces

Names of interfaces are adjectives and should start with an uppercase letter, just like classes. If the name of interface contains multiple words then each word’s first letter should be uppercase. For example,

Student             //name of interface should be adjective
Runnable            //valid

Coding Standards for Methods

Method names are kept on the work they are performing, therefore these names are verbs or sometimes verb noun. Name of method always starts with lowercase letters. If the name of method contains multiple words then first letter of first word should be lower case and the first letter of rest words should be uppercase. Consider the following examples,

run()                   //valid
print()                 //valid
student()               //invalid, name is noun not verb
printdata()             //invalid, first letter of second word should be uppercase
printData()             //valid

Coding Standards for Variables

Just like classes, variable names are intended to be meaningful and noun. Variables should start with lowercase letters and first letter should be uppercase if it has multiple words. For example,

run                //invalid, should be noun
total              //valid
totalsum           //invalid, first letter of second word should be uppercase
totalSum           //valid
rollNumber         //valid

Coding Standards for Constants

In java, every constant names are noun as well as they are all in uppercase. If they contains multiple words then they should be separated by hyphen symbol (). For example:

pi          //invalid
PI          //valid
MAXVALUE    //invalid, should be separated by -
MAX-VALUE   //valid

Note- Constants can be declared along with static and final keywords.

 

Leave a Comment

Your email address will not be published. Required fields are marked *