How to improve code quality in a Pull Request

How to improve code quality in a Pull Request

This article will aid developers to improve the overall code quality of their repository, ship high quality features and bug fixes. There are several factors that are taken into consideration while creating a Pull Request, every commit is important and pivotal in maintaining the overall quality of the codebase.

A single commit can break production, bad code quality can jeopardise the reputation of the firm and invite several security vulnerabilities that can destroy the application.

Build Analysis

Essentially, every application that is developed, will be containerised in the form of virtualisation using Docker or any similar tool. Running the container will make sure that the application is running smoothly despite the new changes added to the codebase.

Normally, Build Container shell scripts and Makefiles are used to create these containers. One crucial way to make sure that the Pull Request is well maintained, is by adding these builds. When there is a new commit, this build will run and make sure that the commit does not damage the code base and the container is running fine.

Several issues can be found while running this build and logs are generated every time, that can be used to analyse the root cause of the failures. Ci/CD tools are used for this purpose.

Unit Test Coverage

Unit tests are extremely important in testing each and every small component that are part of the frameworks used to build your application. Consider a Spring Boot application as an example, there are Controller, Data Access Object and Repository layers as part of the framework. You need to write tests that test each of these layers individually and make sure nothing breaks in the code.

Another important aspect to take into consideration is the test data used to test these components. It has to be consistent throughout the code and it is every developer's responsibility to make sure it passes before committing to a PR.

Now, these unit tests can be enabled in the PR, every time there is a new commit, these tests will run in the backend and make sure that the new commit does not break the codebase.

Automation Test Coverage

Once you build an application, test automation becomes a key factor in keeping it as bug-free as possible. In real world scenarios, e-commerce companies can lose millions of dollars due to issues in the internal applications that are used for end to end processing of their data.

In a Pull Request, it is best practice to enable these automated tests so that when there is a new commit, these tests will run in the backend and make sure nothing breaks in the entire application. It is better to add reference variables and remove hard coding that jeopardise the code quality of these automated tests.

Pre and Post-Merge Conflicts

You can enable these checkers to make sure there will not be any pre-merge or post-merge conflict in the Pull Request. This is very crucial when you open a new Pull Request and some other develop commits a piece of code that could potentially cause conflicts in the branch that you are trying to merge with Integration/main.

Sonar Qube Analysis

This is a game changer when it comes to improving code quality in a Pull Request. There are couple of useful checks that goes through in the PR when Sonar Qube is switched on :

Finding duplicate lines of code

Testing the quality

This will ensure that the code in your repo is clean and new commits do not disturb the existing consistency throughout the codebase.

Minimum Two Code Reviewers

It is beneficial to have minimum two code reviewers, who are also the dedicated owners of that particular repo. This will make sure that the new commits are thoroughly reviewed and comments are properly addressed.

Security Scan

These security scans must run periodically in the Pull Requests to make sure that there are no vulnerabilities due to the new commit. Taking into consideration that these code bases contribute to billions in revenue and any security threat can cause severe damage.

Changelog Request Generation

It is better to use the "Squash and Merge" option and make sure that the commits in the Changelog Request do not get jumbled and cause confusion while deploying to production and maintaining the documents necessary.

Conclusion

These useful points can enable you to improve code quality in your Pull Requests and keep your codebase clean and scalable.

Image Credit

<a href="https://www.freepik.com/free-vector/automation-testing-abstract-concept-vector-illustration-software-testing-solution-development-process-automation-website-developer-optimization-online-service-site-menu-bar-abstract-metaphor_24122199.htm#fromView=search&page=1&position=52&uuid=8ca48795-f75a-46f0-a839-45d49ca96744">Image by vectorjuice on Freepik</a>