Legacy code and Test Driven Development (Part 2)

Yesterday, I wrote about legacy code and how it really was not possible to initially do test driven development, aka TDD. In this post, I will detail a strategy which I feel is good at explaining how to go about testing. I make no guarantees that this is the best way to go about it (please feel free to suggest other tricks you might use), but it works. But these are useful for me in porting Medusa V2 code to Medusa V4 (long story about why not V3, but there are reasons).

Legacy code and Test Driven Development

I am sure many of us have faced the task of trying to do test driven development (TDD) on a legacy codebase. I myself am facing such a challenge, with several tens of thousands of lines of code in a Laravel project which I am helping maintain and is around a decade old. And being severely behind in the packages it uses, the time has come to give the code some TLC, and update it to use the most recent versions of those package (for example, we are moving from Laravel 5 to 12).

Fluent interfaces and method chaining: A potential hidden landmine

Of late, I have been doing some maintenance work on a legacy website which uses Laravel and PHP 7 (work on the upgrade is in progress). In testing, we found a situation where we were trying to call a method on a NULL value. The line of code read as follows:

      $fleet = Chapter::find($this->getAssignedShip())->getAssignedFleet(true);

The code would then go on to properly use the $fleet value, checking for whether or not it was NULL before using it.

Heimdallr... crossing the rainbow bridge.

In my last post, I talked about running into limitations with Django and Python, where I had queries I was wanting to do, but the integration between the RawQuerySets and DRF were blocking me from being able to easily build those queries and route the results to the DataTable.. Well,  I spent a day or two this past weekend recreating the entirety of what I had written in Django and Python  using Laravel and PHP, and I have some observations.

Heimdallr, do we have a bridge to cross?

When I first wrote about Heimdallr, I saw it as a project which was going to be written in Python using Django. I have been using both PHP and Python since their initial releases, but something made that pairing stand out.  First, outside of it using colons with indention to indicate code blocks, I have liked the ecosystem with stronger typing like C/C++, whereas PHP was weaker in its typing.

Datatables: PHP vs. Python with DRF or django-ajax-datatables

I have been using jQuery DataTables for some years now, and have yet to find anything better. With its ability to produce tables with AJAX, built in sorting, filtering and more, I have used it to display datasets of over a million records. The problem comes when writing the backend code to create the DataTable and handle the AJAX requests. Recently, I started writing a Django application to track my job applications, since the spreadsheet was getting unwieldy with hundreds of rows. But unlike using PHP and Zend Framework, I have a ways to go.

PHP Upgrades (aka the joys of running a LTS operating system)

Well, earlier today, I got a reminder that I had not upgraded PHP. Indeed, unlike most of my installs, the virtual host running my WordPress sites was installed from a Live CD, and was running the dated PHP 5.4 version which CentOS/RHEL 7 comes with as a part of their base. It is a joy of running an operating system which comes with "long term support", aka LTS.