Introduction To Coding Standards with Java examples

blog details
author Ranga Karanam November 14, 2019 3 minutes

Having the right coding standards is an important starting point to having great code. What are coding standards? How do you ensure that you are following the best practices around coding standards?

You will learn

  • What is a coding standard?
  • Why do you need coding standards?
  • What are the most important coding standards?
  • How do you ensure that coding standards are adhered to?
  • What are the best practices and processes?

Article Series

This is the second article in a series of eight articles on Code Quality

What Is A Coding Standard?

Coding standards are standards related to

  • How to use a programming language?
  • How to format your code?
  • How to structure your application?
  • How to use a framework or a utility?

Most organizations have coding standards at the enterprise level as well.

Coding standards ensure a certain level of code consistency, when a developer moves from one module to another within a project, or from one project to another within an organization.

What Are Coding Standards Like?

Common points that are addressed by coding standards include:

  • Formatting
  • Complexity of a method
  • Naming conventions for variables, methods and classes
  • The sizes of methods and classes
  • The number of parameters that a method is allowed to have

Important Coding Standards

Let’s quickly review some of the important coding stands.

Sizes Of Methods And Classes

Have small methods and classes.

If you have a large method, then it is quite likely you have violated the Single Responsibility Principle (SRP).

Complexity Of Methods And Classes

Methods and Classes should have as low cyclomatic complexity as possible. A method that has a large number of conditionals and nested loops is difficult to maintain.

Naming Variables / Methods / Classes

How you name the various program elements directly determines how readable your code is.

If you name your variables, methods and classes in a simple and intuitive manner, over 50% of code readability is achieved!

The Number Of Parameters

Limiting the number of parameters generally results in more maintainable code.

Have good tests with great asserts and good code coverage

How do you ensure you have great tests?

  • Have readable tests
  • Have great asserts, to check for conditions in the test code

If you can have good code coverage with good asserts, then there’s nothing like it!

Note that code coverage is just a number. While having 100% code coverage is good, it is more important to have good asserts checking the functionality.

Verifying Coding Standards

Using Static Analysis

You can do static analysis of your source code using tools such as SonarQube. Make sure that you run static analysis as part of your continuous integration builds.

Include static analysis goals in your sprint “Definition of Done”.

You can read more about it here.

Performing Code Reviews

Static analysis has its limitations, since it is done in an automated manner. It cannot determine whether a variable, method or class is named appropriately.

You also need to have a process of periodically reviewing code, mainly through peer reviews.

You can read more about it here.

Do check out our video on the same topic:

image info

Summary

Having the right coding standards is an important starting point to having great code. Having great process around static analysis and peer reviews will help your project meet its coding standards.

Just Released