Skip to content

Faster and smarter debugging of PHP scripts

February 8, 2012

Faster and smarter debugging of PHP scripts

Today nobody really questions benefits of using source level debuggers for any language including PHP – debugger is simply a tool, necessity in everyday programmers work. However, it is interesting to note that code validating tools are much less known and it is those tools that are very valuable addition to debuggers in code development.

Traditionally, in compiled languages, certain part of code validation (type checking, checking for non-initialized variables) are usually taken by compiler, but besides compiler there are also external tools used to conduct this task (for example checkstyle for Java). This article deals with use of one of those tools (Codenizer) during development of PHP applications.

 

Basic question that programmer asks herself/himself when introducing one new tool into development process is “What benefit can I expect from using this?” Answer to this question is very simple – it is dramatic time saver during development, and we all know what that means in today’s world of merciless business competition – more time you save in development of one application faster you can develop next application, which means more money for you.

Usual way of using debugger is after encountering program malfunction to better analyze reasons that led to unexpected behavior and in the end to eliminate those reasons (i.e. bugs). On the other hand, code verification tools are used during program development to pinpoint possible problematic locations which could eventually lead to bugs in program – so debuggers are used to eliminate bugs once they are in program, code validation tools are used to prevent bugs from appearing in the program in the first place.

Simple example

Let’s consider this simple PHP script (schedule.php) to illustrate how those tools work:

  1. <?php
  2. class Schedule {
  3.    private $SID;
  4.    private $SDate;
  5.    private $flights;
  6.    // Set methods
  7.    function set_SID($SID) {
  8.        $this->SID = $SID;
  9.    }
  10.    function set_SDate($SDate) {
  11.        $this->SDate = $SDate;
  12.    }
  13.    function set_flights($flights) {
  14.        $this->flights = $flights;
  15.    }
  16.    // Get methods
  17.    function get_SID() {
  18.        return $this->SID;
  19.    }
  20.    function get_SDate() {
  21.        return $this->SDate;
  22.    }
  23.    function get_flights() {
  24.        return $this->fligths;
  25.    }
  26.    function __construct($sid, $sdate, $flights) {
  27.        $this->SID = $sid;
  28.        $this->SDate = $sdate;
  29.        $flights = split(‘;’, $flights);
  30.        $this->flights = array();
  31.        for ($i = 0; $i < count($flight); $i++)
  32.            $this->flights[] = $i . ‘. ‘ . $flight[$i];
  33.    }
  34. }
  35. ?>

After invoking code validation tool (example how it looks within Eclipse), we are presented with tool’s report:

As we can see in the report are mentioned 3 potentially problematic spots which should be closer inspected by programmer – in two of those there is obviously typo in variable/property name, and in third case tool issued warning about possible performance penalty of using function call in condition part of loop. We can see that it only took less than a second to detect 2 bugs and 1 performance tip, all without leaving development environment or starting program. Compared to classical debugging this is real time saver, not to say how difficult could be to trace those errors with debugger especially if class Schedule was part of bigger project. This is however just small fraction of checks that code validation tools usually performs.

What you need for quick start with code validation for PHP?

First of all you need tool itself (like Codenizer), then some IDE that supports PHP development (Eclipse or NetBeans would be smart choice), and plugin for integrating tool with IDE which can be found in the same location as tool (link).

Conclusion

It would be wrong to think of code validation as of some magic that would forever relieve you of using debugger and make you make bug-free code. Code validation can’t do that for you, but what it can do is to reduce your time spent debugging program by almost 80% and to help you spot the problems that could otherwise go unseen for very long time.

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: