Category: PHP

Testing (during winter solstice)

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:

And this (obviously uncompleted) test:

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!

ImageWorth: Mix your songs with pictures

Last week I’ve been quite busy. I had a small project in mind. Briefly, the idea is to search pictures that match the songs you are listening to. And present them with some visual beauties.

I named this project as: ImageWorth, try it out!

It gets the “recent listened tracks” list from your account, and for every song title it searches for pictures on Flickr. And uses Javascript with JQuery library to show them smoothly.

Regarding the inside: I strongly used Javascript for making it happen (I wanted to practice some js programming). There is a PHP part, which is basically a proxy to let AJAX call directly to and Flickr, but it’s quite simple. The heavy work is done in the Javascript lines, which were quite interesting to develop. This time I used only Object-Oriented programming, and this leads to some (a priori) weird things in js. This language has its own way to work with objects, with a lot of flexibility, but this flexibility leads you to keep more things in mind. For example, when you want to bind a function (which is an object in js) to an even, without losing the reference to the original object (this) where you did the binding on. It’s not easy at first, but when you start to understand how it works, the beauty of closures and the incredible amount of possibilities it means… you start to love it.

By the way, any comment about ImageWorth? I hope you like it!

Class inheritance (without victims)

The extended or derived class has all variables and functions of the base class (this is called ‘inheritance’ despite the fact that nobody died)

From the PHP official documentation

Problem: When a day is not a normal day

Monday morning. I’ve just arrived at the office, and I see my first task to do: fix a bug in a calendar from one of our websites. Actually there are 2 calendars, and one calculation to show the difference in days between the two dates. Somebody discovered a weird behaviour when you select a range of days in the end of March… the difference of days is not an integer number, but a float one!

I isolated the problem, shortening the range of days, until I discovered that the problem is in the last weekend of March. From Friday 23rd to Monday 26th of March, there are 2.95 days (instead of just 3 days). What’s happening that weekend? Why is the last weekend of March somehow weird? Any idea?

If you want to know the solution, see the first comment of this post. Tip: I use a PHP function to convert the dates into integer timestamps (seconds), I substract these integers, and finally I divide by 3600*24, to know the difference in days.

Sometimes “random” is the best choice

…as nature does. Create something from random numbers.

Here are a couple of examples, from a Sunday morning programming session (plus 1 extra hour from the daylight savings change):

Random bubbles
Random trees

Each time you reload the page, a new drawing appears.
To see them correctly, you need Firefox or Opera, because the images are SVG (Scalable Vector Graphics).

Have fun!

Philosophical thought of the week

A morning office conversation:

Tony (a workmate): Is there a PHP function to stop for a moment the execution of a page?
Me: Yes, it is “sleep()”.
Tony: Just what I need, to sleep a little bit. Does it work with microseconds?
Me: No, it works with seconds. For example, if you write sleep(1), it is going to stop for 1 second.
Tony: Ok, thanks, you are a crack.
Me: And, even more, if you write sleep(2), it’ll stop for 2 seconds.
Tony: Why don’t we use it with a negative value? Probably we will speed up our websites!

Metaphysics on programming

Mirror by Jesús One of those things which sometimes made me crazy while programming are quotes (single ‘ and double ” quotes). Let me explain…

Most of the modern programming languages include a way to manage strings, normally it’s surrounding them with quotes (for example, in PHP: $response=”Hi there!”). The question arrives when you want to add a quote as a part of the string… because every language has its own way (and it’s a little problem if you usually program in different languages). For example, in PHP this is correct: , but this doesn’t work: (instead, you must directly use ), and it’s not exactly the same in Javascript. Next, the real problem happens when the user of your program starts to write lots of quotes as part of the input… you have to be really careful, programming some filters.

I imagine a string looking at itself, thinking “is this quote a part of myself or my end?” ;-), like the mirror in the clever drawing made by Jesús (he is a really fantastic artist, you should take a look at his new drawings). What happens when the limit of something is a part of itself? What happens when you see your limit is a part of yourself? Then you can change it, openning it!

By the way, most programmers do the laziest focus: to not allow the user to introduce quotes. But I think it’s the wrong solution, because it’s not a solution at all, but a patch. The professional way to do this is to implement a real solution, so the user can do what he/she wants. You are the person who has to spend time thinking, and not the opposite. This is the reason of this post: I spent the whole day trying to solve this problem in the most elegant form, in a AJAX-powered CMS I’m working on.

Update: At Sunday night I got it. Now I have an “edit in place” system with support of quotes marks, with an easy plus elegant code :-)