Introduction To Coding Standards with Java examples


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.

10 Step Reference Courses

Image Image Image Image Image

in28Minutes is helping 300,000 Learners across the world reach their learning goals. Click here for the complete catalogue of 30 Courses.

Related Posts

Spring Boot Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. This resources will help you learn and gain expertise at Spring Boot.

Spring and Spring Boot Video Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. Here's a list of video tutorials and courses for you

Software Design - Separation Of Concerns - with examples

Software architects and programmers love having Seperation of Concerns. What is it? Why is it important? Let's get started.

Object Oriented Software Design - Solid Principles - with examples

Software design is typically complex. Object oriented design takes it to the next level. There are a number of design patterns and other stuff to be aware of. Can we make things simple? What are the goals to aim for when you are doing object oriented design? SOLID Principles is a great starting point for Object Oriented Design.

Software Design - Open Closed Principle - with examples

Open Closed Principle is one of the SOLID Principles. You want your code to be easily extended. How do you achieve it with minimum fuss? Let's get started.

Software Design - What is Dependency Inversion Principle?

Dependency Inversion Principle is one of the important SOLID Principles. Dependency Inversion Principle is implemented by one of the most popular Java frameworks - Spring. What is it all about? How does it help you design good applications?

Introduction to Four Principles Of Simple Design

With agile and extreme programming, the focus is on keeping your design simple. How do you keep your design simple? How do you decide whether your code is good enough?

Software Design - Single Responsibility Principle - with examples

For me, Single Responsibility Principle is the most important design principle. What is Single Responsibility Principle? How do you use it? How does it help with making your software better? Let's get started.

REST API Best Practices - With Design Examples from Java and Spring Web Services

Designing Great REST API is important to have great microservices. How do you design your REST API? What are the best practices?

Designing REST API - What is Code First Approach?

Designing Great REST API is important to have great microservices. Code First approach focuses on generating the contract from code. Is it the best possible approach?