Code Conventions in JAVA

Bharath K C
4 min readJun 7, 2021
Why coding standards are important?

Code conventions are important for several reasons:

  • Most of the time software is maintained by other engineers than the person who wrote the code.
  • Code conventions improve the readability of the code.
  • When code conventions are followed engineers can understand new code more quickly and thoroughly.

File Suffixes

Java has the following file type extensions:

Java source       ---->  .java
Java bytecode ----> .class

Comments

Comments can be used to explain code and make it more readable. There are three types of comments in Java. Single line Comments, multiline comments, and documentation comments.

// this is a one line comment/* this
is
a
multiline
comment */

Single line comment starts with two forward slashes and ends at the end of the line. A multiline comment starts at a forward slash, followed by an asterisk, and ends with an asterisk and a forward slash. Documentation comments are used when writing code for the project/software package.

Package

The first line of code in most Java source files is a package statement. After that import statements can follow.

package com.kcb; // this is a package statement

package names are written in all lower case to avoid conflicts with names of classes or interfaces. Companies use their internet domain name to begin their package names, for example, com.example.mypackage for a package named mypackage created by a programmer at example.com.

Class

The class name should start with the uppercase letter and should be a noun. Using acronyms is not recommended. Using white space is not allowed.

// Employee is the class name herepublic class Employee { 
// code snippet
}

Interface

The interface name should start with an uppercase letter. It should be an adjective such as Runnable. The rest of the class naming conventions apply to the interface also.

// Runnable is a fuctional interface in Javainterface Runnable {
// code snippet
}

Method

Method name should start with a lowercase letter. It should be a verb such as print(), main(). If there are multiple words camelcase style should be followed. Example for camelcase name getEmployeeData().

public class Employee {
// method
void draw(){
// code snippet
}
}

Variable

Variable names should start with a lowercase letter. It should not start with special characters like & (ampersand), $(dollar), _(underscore). If there are multiple words in variable name camelcase style should be followed. Using one character variable should be avoided for the most part.

public class Employee {
// variable
String userName;
// code snippet
}

Constant

Constant name should be in uppercase letters such as MAX_VALUE. Multiple words should be separated by underscores.

public class Employee {
// constant
static final int MIN_AGE = 18;
// code snippet
}

Indentation

Code lines longer than 80 characters should be avoided. Four spaces should be used as the unit of indentation.

Wrapping lines

When an expression will not fit on a single line, break it according to these general principles.

  • Break after a comma.
  • Break before an operator.
  • Prefer higher-level breaks to lower-level breaks.
  • Align the new line at the beginning of the expression at the same level as the previous line.
longName1 = longName2 * (longName3 + longName4 - longName5)
+ 4 * longname6; // PREFER

longName1 = longName2 * (longName3 + longName4
- longName5) + 4 * longname6; // AVOID

In the above code block, the first is preferred since the break occurs outside the parenthesized expression, which is at a higher level.

Declarations

Number Per Line: One declaration per line is recommended.

int level; // indentation level
int size; // size of table

is preferred over

int level, size;

Do not put different data types on the same line.

Initialization: Try to initialize local variables where they are declared. If the variable value depends on some other computation then no need to initialize.

White Space

Blank Lines: Blank lines improve readability by separating code lines that are logically related. One block line should always be used in the following circumstances:

  • Between methods
  • Between local variables in a method and its first statement
  • Between logical sections inside a method to improve readability.

Blank Spaces: Blank spaces should be used in the following circumstances:

  • A keyword followed by a parenthesis should be separated by a space. Example:
while (true) {
// code snippet
}

Note that a blank space should not be used between a method name and its opening parenthesis.

  • A blank space should appear after commas in argument lists.
  • All binary except . (dot operator) should be separated from their operands by spaces. Blank spaces should never separate unary operators such as increment (“++”), and decrement (“- -”) from their operands. Example:
a = b + c;a++;

Conclusion

This article does not cover everything that there is to know about code conventions. If you are interested to learn more here is a link https://www.oracle.com/java/technologies/javase/codeconventions-introduction.html.

--

--