Coding guidelines in Java :

why coding guidelines :

RAKESH KUMAR SINGH
2 min readJun 6, 2021

We know Java is one of the most popular and widely used programming language. As a developer when we write code then we should flow some coding standard, because when software goes to maintenance then it possible that code will reviewed by some other developer. So as a developer we should make sure that whatever code we are writing should be readable and extendable.

Few of guidelines are :

Naming conventions for classes and interfaces:

All the classes and interfaces should be noun. First letter of class name and interfaces name should be capital and If it contains multiple word than every inner word should start with uppercase. Name of class and interface should have same meaning for which it is created.

Naming conventions for methods:

All the method name should be verb and first letter of name should be lowercase. If it contain multiple word then every word should start with uppercase. Example — print(),sleep().

Naming conventions for variable:

The variables name should be meaningful and one character variable names must be avoided. Variable name should follow camel case rule. If variable is constant then its name should be in all uppercase. In the naming of constant if there is multiple word then we can use underscore(_).

Curly Braces :

Curly braces are used to define the bodies of classes, methods, and loops.Curly brace is applied at the end of the line that starts the class, method, loop, etc., and the closing brace is on a line by itself, lined up vertically with the start of the first line. In any if block if there is only one statement then also put it in curly braces. No blank lines should be present after the opening brace or before the closing brace.

Indentation:

There should be proper indentation in code and the unit of indentation should be 4 spaces. It will increase readability of our code.

White Space:

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

  1. Operator should be surrounded by space character. For example:

The operation should be written as :

a = (b + c) * d;

And not as :

a=(b+c)*d;

2. Reserved word of java should be followed by white spaces. Example -

while (true) {…}

3. Commas should be followed by a white space. Example -

func(a, b, c, d)

4. Colons should be surrounded by white space. Example -

case 1 : break;

5. Semicolons in for statements should be followed by a space character. For example -

for(int i = 0; i < 10; i++) {…}

--

--