User Brian
Brian
Level 41

Java Coding Conventions. Which Ones to Follow and Why

Published in the Random group
82038 members
Knowing and following industry standards and best practices is quite important in any field, and especially in programming with its complex and sometimes chaotic mix of coding languages, tools, approaches and technologies. That’s why a professional Java programmer should be well-familiar with Java Coding Conventions, which is what we are going to talk about today. Java Coding Conventions. Which Ones to Follow and Why - 1

What are coding conventions?

Coding conventions are sets of guidelines for each specific programming language with recommendations on different aspects of software development in this language, including coding style, best practices and methods. Coding conventions are meant to be followed by software programmers who are coding in this language as the quality guides to ensure their code is readable and the proper maintenance of the software by other people is possible. Coding conventions normally cover every essential component of making software in this programming language, including file organization, indentation, comments, declarations, statements, white space, naming conventions, programming practices, programming principles, programming rules of thumb, architectural best practices, and more.

What is the purpose of coding conventions?

There are a number of reasons why coding conventions play an important role in software development.

  • Maintaining unified code style

Following a coding convention allows the software project to be written in a single unified style, which is beneficial in several different ways such as the following.

  • Minimizing software maintenance costs

One of the most important ones is making it easier to maintain and support the software product, as very often the original authors of the program are not the ones who support it. This is important as 80% of the lifetime cost of a piece of software goes to maintenance.

  • Improving software readability

The improvement of the software readability is another major benefit, which also has multiple implications such as simplifying the introduction of new developers to the project and increasing the efficiency of the collaboration of the development team members.

  • Speeding up the work

Finally, having properly written and structured code is necessary for the software development process to go as fast as it is possible.

Java Coding conventions

When it comes to Java, there are two most common and well-known coding conventions: Oracle’s Java Code Conventions and Google’s Java Style Guide coding convention.

  • Oracle’s Java Code Convention

Oracle’s Code Convention is recognized as the most important one for a number of obvious reasons: Oracle’s convention is the official one as Oracle is the owner of Java, as well as one the oldest (the last revision of this document was made on April 20, 1999). Some of the important parts of the Oracle’s Java Code Convention would be the recommendation to use camel case when defining classes, methods, or variables, to start classes with a capital letter and use nouns to name them, while using verbs in imperative form and starting from a lowercase letter for methods, and so on.

  • Google’s Java Style Guide

Java coding conventions from Google are considered to be important due to Google’s status as the learning Internet and tech company with enormous experience in developing all kinds of Java applications. Another important reason is the fact that Google’s Java code convention was updated on May 22, 2018, which makes it more relevant than the code convention from Oracle, especially when it comes to describing relatively new features of Java that were only released as part Java 8 in 2014, such as lambdas and streams. Here’s how the authors of Google’s Java Style Guide describe the content of this coding convention: “This document serves as the complete definition of Google's coding standards for source code in Java. Like other programming style guides, the issues covered span not only aesthetic issues of formatting, but other types of conventions or coding standards as well. However, this document focuses primarily on the hard-and-fast rules that we follow universally, and avoids giving advice that isn't clearly enforceable (whether by human or tool).” “The Google Java Style Guide is a good reference for the most part, but it is a little permissive on some topics. On the other hand, as a Java programmer you must be used to 4 spaces for code indentation, among other things,” said David Rios, a software architect and experienced Java programmer, in a LinkedIn post with some proposed adaptations to Google Java Style Guide of his own.

Most used Java coding standards

Here are some of the most used Java coding standards that can be found in the above mentioned coding conventions from Oracle and Google, as well as other documents of this kind.
  • Follow proper naming convention;
  • Add comments;
  • Identifier means a symbolic name that refers to the name of classes, packages, methods and variables in a Java program;
  • The variable name should be related to its purpose;
  • Name of the method should relate to the method's functionality;
  • Method should not contain more than 50 lines;
  • There should be no Duplicate code in the same class or other class;
  • Declare global variables only if necessary to use in the other methods;
  • Double check creation of static variables inside a class;
  • Avoid accessing variables directly from other classes instead use getter and setter methods;
  • All business logic should be handled in the service class only;
  • All DB related code should be in the DAO classes only;
  • Use getters and setters;
  • Declare instance Variable as private;
  • Keep the scope of variables minimal;
  • Assign meaningful names to variables;
  • Avoid memory leaks by releasing database connections when querying is complete;
  • Try to use Finally block as often as possible;
  • Use the Executor framework for multithreaded programming.
What else to read:
Java Coding Conventions. Which Ones to Follow and Why - 2
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION