I've had to take over a lot of crappy code bases in my life and they always have their problems. Poor class (or lack thereof) structure, poor error handling, crappy UIs, lack of unit testing, and the list goes on. One of the more difficult things to deal with is the code that has, for one reason or another, been left in the code base but doesn't actually run. This is called Zombie Code because, much like the popular monster, it's not really dead and it's not really alive it's just there waiting to bite your face off when you least expect it (by deleting hundreds or thousands of records). Here are some of my favorite varieties.

If(false) Zombies

This type of zombie code consists of an if statement that never evaluates to true.

if($alwaysPositiveNumber < 0){

if(isset($_REQUEST['neverSentVariable'])){

if(2 == 3){ // no shit, I've actually seen this

This is the worst type of zombie code for two reasons. The first is that by just quickly looking at the code it's very hard to tell the code doesn't run because the if statement could contain tens or hundreds of lines of code. The second is because this could be a case where the condition never occurs because someone deleted an option at some point and when you add it back in (or a user enters a strange value) it causes the code to come back to life.

Comment Zombies

This type of zombie is a large chunk of text commented out.

/* $myVariable = 'something';
<snip a bunch of lines
someReallyImportantFunction(); */

It's worst when it starts with:

// removing this by request but leaving it in in case it's needed

I find that this generally happens in projects where there isn't version control or there is a distrust of version control.

This type of zombie is annoying because even through it doesn't run it still shows up in searches which wastes time. Whenever I see something like this I delete it immediately but create a commit specifically for the deletion.

Unused File Zombies

This type of zombie consists of files that are just not used anymore. Generally, it's whole features that for reasons lost to time aren't needed anymore.

The best solution to this is to just delete the file and if it's every needed again it can be restored from version control.

Unused Database Fields Zombies

As the name implies this type of zombie consists of database fields and tables that aren't used any more. These can be hard to clean up because they might actually contain information and it can be politically difficult. They add to the mental weight when developing the software so it's best to remove them.

I suggest a three step process for dealing with these items.

  1. Verify that the column isn't used anymore (this can be done by adding "old" or "deleteSoon" to the end of the column name)
  2. Create a backup of the files using mysqldump or creating a CSV file with their content
  3. Delete them

Inaccessible Properties Zombies

This is another case where a feature existed and it was either replace or removed but not everything was cleaned up and again the best solution is to just delete the property after checking to make sure it's not used anywhere. Version control makes it much easier to do this and move on with our lives.

What Can We Do To Prevent These?

Zombie code is just another type of technical debt and like all technical debt it needs to be paid down. The best solution would be to have your team take a sprint or a month and just work on getting rid of code that isn't used any more. I know that's not always possible to spend a long chunk of time not shipping new features but some times it just needs to happen. If a whole sprint isn't possible maybe take a day at the beginning of the next 14 sprints to accomplish it.

After you've paid down your debt you'll need to maintain. At this point, your team needs to pledge that they're not going to allow this to happen again. They'll need to remove all of a feature when it's removed and keep themselves from leaving unneeded files and database fields. I would recommend looking into using code coverage tools like those bundled with PHPUnit and other unit testing tools.

Are there types of deal code that I haven't encountered? Leave a comment below.