The Baseline Approach: A Game-Changer for Legacy Code
Tags: programming, refactoring, php, codequality
Published: 2023-11-29 16:35:45

In the world of software development, starting a new project from scratch is often exhilarating. It presents an opportunity to do everything right from the outset. However, it's a common experience that today's greenfield projects quickly evolve into tomorrow's legacy codebases. The challenge then becomes how to modernize these legacy systems without the luxury of starting anew, constrained by time and budget.

This challenge transcends programming languages, affecting developers in every ecosystem. The key to sustainable modernization lies in the adoption of 'baselines' – a strategy effective across various programming environments.

## What is a Baseline?
A baseline serves as a fixed reference point for the current state of a codebase. It acknowledges the existing code as it is, even if it's not ideal. This pragmatic approach allows developers to manage legacy code without being overwhelmed by it. The baseline sets the stage for incremental improvements, enabling teams to introduce and enforce new coding standards and practices for all future code, whether it involves new files or modifications to existing ones. This method ensures gradual modernization without the need to overhaul everything simultaneously.

## Baselines in Practice: PHP and Beyond
The concept of baselines is well-integrated into the PHP ecosystem. For example, it's a feature in [PHPStan](https://phpstan.org/) and [Psalm](https://psalm.dev/), two popular static code analyzers for PHP. This allows you to enforce much stricter PHPStan/Psalm levels or individual rules on new code while also creating a full catalog of code smell in your codebase which you then can fix overtime. Another great benefit of baselines is that they make updates of such static code analyzers much easier, as you can upgrade immediately and solve the newly popping up errors over time.

Besides static code analysis, I recently also explored that there exists a [baseline library](https://github.com/iodigital-com/php-code-sniffer-baseliner) for [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) one of many PHP Code style formatters. On first sight this does not make too much sense as code style should just be applied to the whole code base, if legacy or not. And that's technically true, but those days there also exists code style rules to enforce certain ways of doing stuff in PHP that are not only code style but could possibly also produce breaking changes by fixing it. Think about unsafe comparison operators or strict type declarations. You might want to enforce as much strictness as possible for new code to make your life much easier in the long run, but you don't have the time to upgrade your complete code basis to such rules. Especially because possibly introduced bugs are really difficult to spot.

However, the concept of baselines isn’t confined to PHP. It can be just as effective in other programming languages, whether it's JavaScript, Python, Java, or Ruby. There is a good chance that a baseline implementation is already available for the tools you use daily.

## Your Thoughts and Experiences
I'm keen to hear your thoughts on the baseline approach. How do you see it fitting into your development workflow, regardless of the programming language you use? Are there tools in your language of choice that currently use or could benefit from implementing baselines? Your insights and experiences with this approach across different languages could be invaluable to the broader developer community.