Making It Easy to Locate and Remove Test Code

When troubleshooting, sometimes it's necessary to add code to test various features. The problem is that the code may accidentally be left in when going live. Removing the extra code as you go may work in most situations. Or maybe you just know where the test code is located and removing it doesn't seem like a problem. However, what happens if you're pulled away from the task by some other emergency or deadline? Over time, the test code you were so familiar with may not be as obvious. Instead of depending on memory, here are some options for making test code stand out.

Giving Some Space

Inserting line breaks before and after our test code is a simple way of distinguishing it from the rest. Let's say our script uses the current date to grab the upcoming events from a database. To make sure everything works as expected, we'll override the variable used today's date so it can be changed dynamically.

<?php
//...
$today = date('Y-m-d');
 
 
$today = $_GET['today'];
 
 
$sql = "SELECT fields FROM table WHERE date>='$today'";
//...
?>

Now, the date can be set to any value via the website URL. For more information on this testing technique, see "Setting up a Makeshift Test Environment for Experimenting with PHP Code." When the testing phase is over, we can scroll through the script looking for the extra white space to remove the unneeded code.

Commenting with Keywords

Adding space may be an okay for some situations. However, we may want to specifically state that the chunks of code are used for testing purposes.

<?php
//...
$today = date('Y-m-d');
 
//TEST code; remove when done-------------
$today = $_GET['today'];
//----------------------------------------
 
$sql = "SELECT fields FROM table WHERE date>='$today'";
//...
?>

Now, if we're away from the project for an extended period of time (or if we're in a team environment), the test code should be easy to pick out. Note: as long as the comments say the same thing, all the test code could be found with a quick search.

Conclusion

It doesn't matter whether you use comments, spaces, or some other option. The important thing is to make going live as foolproof as possible. It may take time getting used to the new procedure, but it's worth it.

0 Comments

There are currently no comments.

Leave a Comment