Introduction To Coding Standards with Java examples

Image Image

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?

Free Courses - Learn in 10 Steps

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


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.

Best Selling Udemy Courses

Image Image Image Image Image Image Image Image Image Image Image Image

Join 450,000 Learners and 30+ Amazing Courses

350,000 Learners are learning everyday with our Best Selling Courses : Spring Boot Microservices, Spring, Spring Boot, Web Services, Hibernate, Full Stack React, Full Stack Angular, Python, Spring Interview Guide, Java Interview, Java Functional Programming, AWS, Docker, Kubernetes, PCF, AWS Fargate and Azure

Do not know where to start your learning journey? Check out our amazing learning paths:
Learning Path 01 - Spring and Spring Boot Web Applications and API Developer,
Learning Path 02 - Full Stack Developer with Spring Boot, React & Angular,
Learning Path 03 - Cloud Microservices Developer with Docker and Kubernetes,
Learning Path 04 - Learn Cloud with Spring Boot, AWS, Azure and PCF and
Learning Path 05 - Learn AWS with Microservices, Docker and Kubernetes



Related Posts

Understanding jwt token

Let us understand the json web tokens

Single sign-on workflow

Let us understand the single sign-on workflow

Oauth2.0 - Resource Owner Password Credentials grant workflow

Let us understand the Oauth2.0 Resource Owner Password Credentials grant workflow

Oauth2.0 - Implicit grant workflow

Let us understand the Oauth2.0 implicit grant workflow

Oauth2.0 - Client Credentials grant workflow

Let us understand the Oauth2.0 client credentials grant workflow

Oauth2.0 - Authorization grant workflow

Let us understand the Oauth2.0 authorization grant workflow

Writing Integration Tests for Rest Services with Spring Boot

Setting up a basic REST Service with Spring Boot is a cake walk. We will go one step further and add great integration tests!

Integrating Spring Boot and Spring JDBC with H2 and Starter JDBC

Learn using Spring Boot Starter JDBC to connect Spring Boot to H2 (in memory database) using Spring JDBC. You will create a simple project with Spring Boot. You will add code to the project to connect to a database using Spring JDBC. You will learn to implement the basic CRUD methods.

JUnit Tutorial for Beginners in 5 Steps

JUnit Tutorial for Beginners in 5 Steps. Setting up a basic JUnit example and understanding the basics of junit.

JPA and Hibernate Tutorial For Beginners - 10 Steps with Spring Boot and H2

JPA and Hibernate in 10 Steps with H2 - Setting up a basic project example with Spring Boot and in memory database H2. Its a cake walk.