Monthly Archives: December 2008

Testing (during winter solstice)

NOTICE: Find more in my tech blog: liopic.me.

What was I doing during the shortest day (in Northern Hemisphere) of the year?
Enjoying the “code coverage” used in testing, while traveling to my hometown by train for Xmas.
Testing, code coverage… what’s that? you ask. Let me explain.

A really important part of developing code is testing the code you write. It’s the way to verify your code really works. The easiest way to do so is inserting some “prints” here and there, to verify that variables store expected values. Or you can write an external program to stress your code with some inputs and verify expected outputs. But the best way is to use a testing tool for creating automated tests. Then you normally do some unit tests (that stress modules/objects) and test suites (that aggregates unit tests).

Until recently I didn’t use automated tests. But some months ago I discovered PHPUnit, and now I’m “safe”. Automated tests helps you and your team against careless modifications (made by this new internal, or by yourself in a bad day).

And what’s code coverage? Well, when you write tests, there is a way to see if your tests stress all the code lines: code coverage. But let me show you a really small example of this useful tool.

Imagine this function/method:

function isChristmas($day,$month)
{
    if($day==25 && $month==12){
        return "Merry Christmas";
    }else{
        return "Have a good day";
    }
}

And this (obviously uncompleted) test:

public function testChristmas()
{
    $this->assertEquals("Have a good day",isChristmas(12,12));
    $this->assertEquals("Have a good day",isChristmas(28,02));
}

When you launch PHPUnit and ask for a code coverage analysis in HTML, you get something like this (click to see it fullsize):
Code coverage with PHPUnit
Isn’t this wonderful? You get a lot of information! You quickly discover ways to improve your tests and sometimes your source code… how could’ve lived without this?

Merry Christmas, by the way!

Professional PHP: A difficult task, and even worse in Spain

NOTICE: Find more in my tech blog: liopic.me.

PHP ElephantThis is a post about tech books. Sometime ago I discovered that Internet is not, and should be not, the only information source for an IT professional. It’s a good place to search for technical reference, to scan for chunks of information, but definitively it isn’t a place to read long texts. Books are the alternative. We get used to scan text on Internet (Nielsen dixit), but we read books in other way, in a more relaxed way. And books authors write books in a different way from writing on Internet. We need books (or big and comfortable e-books) in order to understand “the big picture”.

PHP is a flexible language, and thanks to its version #5 it’s a modern object-oriented language. It’s also an easy language to learn, which leads to help newbies. But, just because this, most of the PHP programmers’ population have a low programming level. This could be a reason to explain why almost every PHP book seems written for newbies. PHP5 is object-oriented so, as an example: how many books explain Design Patterns using Java? More than 30 (according to an Amazon search). How many with PHP5? Just 4. Another example… let’s speak about unit testing: how many books are out there on jUnit? Around a dozen. And on PHPUnit (something like jUnit, but in PHP)? Just ONE!

And things get worse if we speak about trying to buy advanced PHP books in Spain. Libraries in Spain avoid books written in other languages but Spanish, and of course none of the “advanced level” PHP books get a Spanish translation. So the reality is that it’s almost impossible to get a decent book. Last year I was surprised when I went to London, entered in a just normal library and found a lot of hi-tech PHP and AJAX books. I tried to found a good library here in Barcelona, but it’s impossible: so finally I ordered some books to Amazon UK!