patpitchaya - Fotolia

Follow these best practices on how to write clean code in Java

Follow these three best practices to write clean Java code and help make your application development simple and painless for all parties involved in the process.

The ability to write clean code is important no matter which programming language you work with. When you work in Java, it's even more pertinent.

Java has long been the world's most popular programming language, and Java codebases tend to have long lifespans. Many applications written in Java are extended and maintained by people other than their original authors. This means the Java code you write today has a good chance of being read and understood by someone other than you -- possibly years or even decades from now. For that reason, it's important you know how to write clean code in Java.

At the same time, clean code doesn't come easy. Compared to many other popular programming languages -- such as C and Python -- Java is more verbose. You have to write more Java code to implement the same amount of functionality, and the more code you write, the harder it becomes to keep it lean, mean and clean.

Let's examine three best practices on how to write clean code in Java.

Follow Java style conventions

Style conventions refer to coding practices that programmers follow to create consistency in the way code is written and organized, and these are important.

The problem with Java style conventions is that there are multiple style guides out there. Although the various guides overlap in many of their recommendations, they also conflict with each other in some important respects.

For example, take implementation comments. Oracle's style guide -- the oldest major Java one in existence, and probably the most used by developers -- says that block comments should be indented and preceded by a blank line. In contrast, Google's Java style guide -- which has become the go-to reference for more and more Java developers in recent years -- specifies that block comments have the same indent as the surrounding code, and says nothing about preceding them with blank lines.

The Oracle and Google style guides are the most influential for clean Java code, but there are various other Java style guides created by other sects of the programmer community.

So which style guide should you follow? Choose whichever one makes the most sense within the social and economic context of the code you write. If your organization already has chosen a style guide, then obviously follow it. Consider asking a company which style guide it uses during an interview to present yourself as a serious and organized programmer.

If you create classic enterprise Java applications, Oracle's style guide probably makes the most sense. If you focus more on mobile apps, which are oriented toward the world of Google and Android, I'd recommend you follow Google's style guide. If you develop scientific apps, look for an academic Java style guide, such as one from a college or university.

Regardless of which Java style guide you choose to follow, the most important thing is simply to be consistent. Don't switch between one style and another within the same application because there's no automated or easy way to change Java code from one style to another. 

Minimize class size

Programmers like simplicity. One way to keep Java code clean and simple is to keep your Java classes as small as reasonably possible. As a general rule, it's better to have an application that consists of many small classes than a few large ones.

When it comes to actual lines of code per class, different Java programmers will make different recommendations. Some say that a class shouldn't be larger than around 900 lines of code, while others suggest 2,000 lines as an upper limit.

Thus, there is no hard and fast rule about Java class size.

Use your best judgment to decide how large to make your classes, based on the context in which you work. If you write a short Java program, it might be better to keep everything within a single class to simplify your code for other programmers to follow. Also, avoid too many classes. Otherwise, a programmer would have to look through a dozen of them to figure out how just one part of your application works.

You should follow the same logic when it comes to the size of functions and methods, too. There is no official rule about how large they should be, but it is generally wise to err on the side of shorter rather than longer.

Give variables and methods logical names

Another best practice on how to write clean code in Java is to assign logical, human-readable names to methods, variables, functions and other objects within your code.

Sure, it's faster to assign names of a single character to your objects or to type out whichever random name first comes to mind as you code. But these habits make it more difficult for other programmers to figure out what role the object plays within your application.

Likewise, if you give variables or methods generic names -- like var1 or methA, for example -- it forces other programmers to think harder to read your code. That's the exact opposite of clean Java code.

Instead, use names that reflect the intended functionality of the variable, method or function you've written.

There are a few other best practices related to variable, method and function names that are worth noting:

  • Almost all Java programmers use lowerCamelCase to determine which characters to capitalize within method and variable names. Do so unless your style guide instructs you to do something different.
  • Use names that are easily pronounceable. This reduces the amount of mental labor that others have to perform to scan your code. Ideally, the names will be easy to pronounce even for people who aren't native speakers of your language.
  • Use verbs for the names of functions and nouns for the names of classes and variables.

At the end of the day, knowing how to write clean code in Java requires more than a little judgment and planning by programmers. There are no universal rules to follow, but there are some best practices that, by and large, will help you to craft clean code in Java.

Dig Deeper on Development tools for continuous software delivery

App Architecture
Software Quality
Cloud Computing