Improve your code quality effortlessly

Michal Chylik
UFG news
Published in
3 min readSep 6, 2021

There are many ways to improve the quality of code your developer team produces. The easiest to implement and maintain is the integration of static analysis tools into your development process.

Photo by Caspar Camille Rubin on Unsplash

Create your own Coding Style Guide

First step towards cleaner and more understandable code is to create Coding Style Guide for you team/company. Don’t reinvent the wheel. Look for the existing ones for your technology and choose the one you prefer. You can tweak it a bit, to match your preferences.

Our main technology is PHP, so we used PSR-12 as a cornerstone for our coding style guide and added few rules from Pear and Symfony coding standards. This draft of our guide was then discussed with the whole dev team. Following discussions and first weeks of practice resulted in few more custom rules added, but we now have a proper Coding Style Guide to follow.

How to enforce the rules

It would be very difficult, if not impossible, to check all the rules manually by developer or code reviewer. Fortunately, there are plenty of tools that will do static analysis for you. They can be customized to follow your own coding style guide. Many of them can even fix the problems automatically for you!

The set of tools depends on your technology stack. For our purposes we used following ones:

You shall not commit ugly code!

We decided to use GrumPHP, which allows us to run all the tools listed above with one command. GrumPHP can be run from command line, but its most common usage is to run tests on the committed code. It will automatically register some git hooks in your repository and run defined testsuites on the code you want to commit. If any of the tests fails, you will see this grumpy face.

This grumpy face will not let you commit!

This tool is really powerful and there are many possibilities how to customize it for your purposes. I will get into more details of our implementation (GrumPHP + Magento) in one of my following articles.

Another reasonable point in your development process, where you could run these static analysis tools, is deployment pipeline. You can always bypass the tests on your commit if you want to, so final check is a good idea. But, in my opinion, it is too late (and therefore too expensive) to run first checks in deployment process. So the best way to go is to make initial check on commit and final check in deployment pipeline.

But what about existing projects with huge codebase?

Tools like GrumPHP, which are able to run tests on committed code, are easy to implement even for huge monoliths. You can continually improve your codebase as you work with existing code. Downside is, you won’t be able to add final check into your deployment pipeline, as it would check the whole codebase. But making sure that all committed files will now follow your coding style guide is a big improvement!

Benefits of using static analysis tools

There are plenty of benefits, but these are the main ones:

  • Code is more consistent
  • Less hidden bugs
  • Code is more readable
  • Code is more predictable
  • Shorter learning curve for new developers
  • Improves productivity

Static analysis tools are really the low-hanging fruit. Easy to implement and benefits are obvious since day 1. So what is YOUR excuse for not using them?

Looking for more ways of improving your development process? I wrote about Code reviews in my previous article.

--

--

Michal Chylik
UFG news
Editor for

Tech Lead always keen to learn and write about new technologies.