4 Java coding style Google suggests to follow

4 Java coding style Google suggests to follow

We are going to list down a few very basic standards which we should follow. These standards are being taken from the Google Java Style Guide.

1. Source file Structure→

Imports are ordered as follows:

  • All static imports in a single block,
  • All non-static imports in a single block.

Wildcard imports, static or otherwise, should not be used.

Logical order needs to be maintained

Always try to maintain logical order or method inside a class. By logical order, it should be like a storybook. If someone is reading your class they should find it easy to locate methods depending on the requirement.

One thing we try to follow is always adding methods in last to maintain an order of how classes are updated over a period of time. But sometimes this breaks logical order and becomes very difficult to read in class.

When a class has multiple constructors, or multiple methods with the same name, these appear sequentially, with no other code in between (not even private members).

2. Formatting

Braces are used with if, else, for, do, and while statements, even when the body is empty or contains only a single statement.


Avoid:
if(true) System.out.println("True");

Good to have
if(true) {
    System.out.println("True");
}        

  • No line break before the opening brace.
  • Line break after the opening brace.
  • Line break before the closing brace.
  • Line break after the closing brace

Avoid:
if(true) 
{
    System.out.println("True");
}        

Horizontal alignment: never required

private int x; // this is fine
private Color color; // this too

private int   x;      // permitted, but future edits
private Color color;  // may leave it unaligned        

3. Variables

One variable per line →Every variable declaration (field or local) declares only one variable: declarations such as int a, b; is not used.

Declared when needed → Local variables are not habitually declared at the start of their containing block or block-like construct. Instead, local variables are declared close to the point they are first used (within reason), to minimize their scope. Local variable declarations typically have initializers or are initialized immediately after declaration.

No C-style array declarations → The square brackets form a part of the type, not the variable: String[] args, not String args[]

4. Annotations

Annotations applying to a class, method, or constructor appear immediately after the documentation block, and each annotation is listed on a line of its own (that is, one annotation per line). These line breaks do not constitute line-wrapping (Section 4.5, Line-wrapping), so the indentation level is not increased. Example:

@Override
@Nullable
public String getNameIfPresent() { ... }        

single parameterless annotation may instead appear together with the first line of the signature, for example:

@Override public int hashCode() { ... }        

Annotations applying to a field also appear immediately after the documentation block, but in this case, multiple annotations (possibly parameterized) may be listed on the same line; for example:

@Partial @Mock DataLoader loader;        

Thanks To : javarevisited

To view or add a comment, sign in

More articles by Omar Ismail

Others also viewed

Explore content categories