Open In App

Coding Guidelines in Java

Java is one of the most popular and widely used programming languages and platforms. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable, and secure. From desktop to web applications, scientific supercomputers to gaming consoles, cell phones to the Internet, Java is used in every nook and corner. 



In this article, let us understand a few coding guidelines that help to increase the readability of the program. 

Why Coding Guidelines? 

The coding guidelines are important because most of the software cost goes towards maintenance. And also, the software is not always developed by a single developer. Therefore, maintaining a convention for writing software increases the readability of the program. 



A few of the guidelines are: 

1. Naming Conventions: We generally follow the camel case convention in java programming. It means that all the classes and interfaces should be nouns, in mixed cases with the first letter of each internal word capitalized. All the methods should be verbs, in mixed case with the first letter lowercase and with the first letter of each internal word capitalized. The variables should be meaningful and one character variable names must be avoided. A constant variable is defined in the capital case.

2. Curly Braces: Curly braces are used to define the bodies of classes, methods, and loops. There are two standard formats for the usage of curly braces, either of which is used. 

class Geeksforgeeks {
   ... Geeksforgeeks(){
    // Constructor
       ...
   }

   int Geek(int a, float b){
   
       ... for (int i = 0; i < Field; i++){
           ....
       }
   }
}
class Geeksforgeeks  
{
   ... Geeksforgeeks()
   { // Constructor
       ...
   }
   int Geek(int a, float b)
   {
       ... for (int i = 0; i < Field; i++)
       {
           ....
       }...;
   }
}

3. Indentation: The unit of indentation should be 4 spaces. Tab-stops should be set exactly every 8 spaces. All indentation must be achieved by the space character and tab characters must not exist in the resultant source file. The recognized standard for increasing readability of each line is: 

class Geeksforgeeks {
   private int s;
   private double d;

   Geeksforgeeks()
   { // Constructor
       s = 1;
       d = 3.14;
   }

   int Geek(int a, float b)
   {
       // Must initialize local variables
       int l = 0;
       float le = 1;
       int n = 10;
       l = n - 2;
       le = l + b * 3;

       for (int i = 0; i & lt; n; i++) {
           l = l * 2;
           l = l - n;
       }
       return l + a;
   }
}

4. White Space: White spaces also play a major part in readability as follows: 

The operation should be written as: 
a = (b + c) * d;
And not as: 
a=(b+c)*d 

The loop must be initialized as: 
while (true) {…} 
And not as: 
while(true){…} 

The functions must be initialized as: 
fun(a, b, c, d); 
And not as: 
fun(a, b, c, d); 

The case statements must be initialized as: 
case 100 : break; 
And not as: 
case 100:break; 

The for loop must be initialized as: 
for (i = 0; i < n; i++) 
And not as: 
for(i=0;i<n;i++) 

5. Comments: Java programs can have two types of comments. They are Implementation and Documentation. Comments should contain only the information that is relevant for reading and understanding the program. For example, information about how the package is built or in what directory it resides should not be included in the program as a comment.

// block comment on line 1 
// block comment on line 2 
// block comment on line 3 

a = 10; 
b = 20;
// a single-line comment 
c = a * b; 

if (a == 2) {
   b = true; // special case
}
else {
   c = isPrime(x); // works only for odd
}
if (a > 1) {
b = a; // + 1;
...
}
else {
// b = 2;
...
}

/** This is a java documentation comment */
private int comments_;

Documentation comments are meant to describe the specification of the code, from an implementation-free perspective, to be read by developers who might not necessarily have the source code at hand. Java associates documentation comments with the first declaration after the comment. As a result, documentation comments should not be present inside a method or constructor definition block. For example:

Though we can say that the above-mentioned guidelines are not definitive and they are relative, but it is always preferred to maintain the guidelines because the software is never developed by a single person and might not be maintained by the same team who has developed the software. In order to solve any bugs in the software, the deployed code must be easily readable. Following the above guidelines makes the code readable not only for the developer but also for a new person who is reading the code for the first time.


Article Tags :