Learn Java

Java Coding Style Conventions

There's a question that you bump into when moving from simple single-line programs to more complicated ones: "How can you write code that is clean and easy to read?" This is trickier than it may seem at the beginning and it is quite important too. In real life, programming is a process that involves a lot of people who work together interacting with each other's code. In fact, you often spend more time reading code than writing it. Even when you're working alone and writing a program "for yourself", after a while, it may become difficult for you to understand your own program if it's badly written.

That is why you need to follow common best practices concerning programming style, which are basic rules for writing clean code. This way, other programmers and yourself can read your code easily and avoid introducing errors. By writing good code you can impove readability and efficiency, and it may help you get your first job and make a good impression on your colleagues.

"Good coding style is like correct punctuation: you can manage without it, butitsuremakesthingseasiertoread." – The Tidyverse Style Guide by Hadley Wickham

Java Conventions

A list of recommendations on how to write code for some particular language is usually called a coding style guide or style conventions. These conventions help developers standardize and support well-readable code. They are more like recommendations than strict rules, but by following them a programmer creates code that is clean and consistent so that other developers will be happy to work with it.

In most cases, companies and individual developers do not create their own style conventions. There are two generally accepted Java conventions that are used all over the world:

Sometimes these conventions can be modified or extended by a particular company to meet their needs.

In all our code examples and exercises, we will follow the Oracle Code Conventions and we urge you to do the same while learning here. After completing the course, you can follow any convention you want. Actually, it doesn't really matter which one to follow, the main point is to be consistent across your code.

There is no need to learn all the conventions at once: just remember to open them from time to time after learning some new syntactic concepts. We will provide the information throughout the course when needed.

Now let's look at some of the most basic Java conventions according to the Oracle Code Conventions.

The number of spaces

The first convention is to use 4 spaces as the unit of indentation in the whole program code. You have already seen our code examples before and you might have noted that we used this value in our examples.

Good:

1public class NumberOfSpacesExample {
2
3    public static void main(String[] args) {
4        System.out.println("Hi!");
5        System.out.println("I'm a Java program.");
6    }
7}

Very bad:

1public class NumberOfSpacesExample {
2
3   public static void main(String[] args) {
4         System.out.println("Hi!");
5System.out.println("I'm a Java program.");
6 }
7  }

As you can see, the second code example, with its irregular indentation, looks ugly and requires some effort to be read.

Sometimes tabulation is used to create an indentation. However, tab may correspond to 8 spaces instead of 4 in some IDEs. That is why we recommend you stick to spaces in this course.

The location of curly braces

Some time ago, developers were arguing a lot about where to put opening and closing braces (or curly brackets if you will) in C-like programming languages. The next convention describes what to do in Java:

  1. Put the opening curly brace at the end of the line where the block begins.
  2. Put the closing curly brace at the beginning of the next line.

There are two examples below which illustrate these rules.

Good:

1public class NumberOfSpacesExample {
2
3    public static void main(String[] args) {
4        System.out.println("Hi!");
5        System.out.println("I'm a Java program.");
6    }
7}

Not that bad, but not the Java-way:

1public class NumberOfSpacesExample 
2{
3    public static void main(String[] args) 
4    {
5        System.out.println("Hi!");
6        System.out.println("I'm a Java program.");
7    }
8}

Here, the second code example doesn't look ugly. However, it is simply not the way code is written in Java. Most of the common conventions follow the first example.

Avoid extra spaces

Sometimes you may add some spaces even if you don't really need them. This will make your code difficult to understand and reduce readability.

  • Avoid extra spaces within parentheses

Good:

1System.out.println("Hello!");

Bad:

1System.out.println( "Hello!" );
  • Avoid an extra space before an open parenthesis

Good:

1System.out.println("OK");

Bad:

1System.out.println ("Shifted braces");
  • Avoid extra spaces before a semicolon

Good:

1System.out.println("No extra spaces");

Bad:

1System.out.println("It has an extra space") ;

The length of a line

The last recommendation concerns the maximum length of a line. The Oracle Code Conventions proposes avoiding lines of code longer than 80 characters. Plenty of developers consider this restriction as outdated since modern monitors can easily display longer lines, whereas others would go on following this rule, which is handy, for example, when coding on laptops.

Other popular character limit values are 100, 120, and sometimes even 140 characters.

Conclusion

Style guides provide the conventions to help create easy to read and consistent code. For Java, the two most popular ones are the Oracle Code Conventions and Google style guide. One of their main objectives is to provide an effective way for developers to work together on code. Because of that, it is not as important to strictly follow one of the existing style guides but to stay consistent within the project. Later on, you will learn a lot of things about Java and become a skillful programmer, during which maintaining the consistent code style will always remain an important priority. Do not worry too much as you do not need to learn all the conventions at once. In all the following topics, we will follow the Oracle Code Conventions and encourage you to do it with us!

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate