Category: Development

Feeling as a senior programmer

Here it is a morning conversation with my new Hindi junior coworker. He is in New Delhi, working for us remotely, and I’m teaching him quite a lot of things, and love to discuss about programming problems…

Me – have you read about Composite Pattern?
He – i use to say to my friends that am good at programming, then I found you :(
Me – hahaha
He – right now reading Pragmatic Programmer, :)
Me – really? really good
He – planning to apply for zend certification so I’ve so much to study
Me – This book changed the way I think about programming

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!

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

This 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!

“Hello World!” is too hard to program

10 years ago, when I saw for the first time a Java program, the classical “Hello World!”, I thought for a moment “booo, it’s counterintuitive… you need to create a class just to say Hello”. Later I used to do all the nasty work with classes.

But I never imagined something like the following:

Malbolge, invented by Ben Olmstead in 1998, is an esoteric programming language designed to be as difficult to program in as possible. The first “Hello, world!” program written in it was produced by a Lisp program using genetic algorithms.
– from Malbolge description

8-0 No comments.

Playing with the bits of double variables

[Most of my readers will not understand the following, but the mathematicians will love it]

A couple of years ago, a mathematician friend told me some problems he had about optimizing a program (maths calculations), asking me for suggestions. I answered with some ideas, but then he asked me about the double format, the way the computer stores a double variable, with its separated sign, mantissa and exponent (actually, IEEE 754). He was pretending to manipulate directly those parts, to improve the calculation’s speed. I didn’t help him in a clear way.

In the last few months I’ve been taking part in some programming contests, on TopCoder. Today one of the problems was really boring but at the same time really interesting. In brief, it says that the division is a computationally expensive operation, except if the divisor is a power of 2 (then you can use just bit shifts to do so). The statement asks to program a division, approximating the result using a series of divisions by powers of 2.

I tried to use bit shifts and bit masks to get the exponent of a double (keeping in mind the double binary format), but I discovered that C++ (as C) doesn’t allow bit operations with doubles.

Nevertheless later I asked in the forums, and got an interesting idea. Using an “union you bind two or more variables, sharing the same memory position. That way you can manipulate the value in memory from different angles.

Here is an example in pure C:


KISS : Keep It Simple and Stupid

Sometimes I forget this principle. The other day was one of such days. I was trying to solve a problem at TopCoder (a website that runs computer programming contests)…

The statement was something like “given n males and m females, sit them down in a circular table, in a way that if you start removing every K persons, after m steps there will be only males”. It looks like a trivial problem (and it is), but the difficulties arrive when you consider a big K, starting to do loops with a different number of people each time.

I started using a lot of modulus operations and trying to get an elegant solution as well. But the modulus is quite expensive (in computational terms), and the code was getting confused (and of course it was really difficult to trace). I made a lot of examples with pen and paper, trying to abstract something useful. Suddenly I realized: why don’t I program just what I am doing with pen and paper? Just letting the computer iterates every step, counting and setting females at the K step. I programmed it, and later I verified (peeking other’s code) that it was the correct solution: simple, stupid, but effective. [Well, actually the complexity in this way is O(m*K), and using only modulus is O(m), but in a short domain (as they said, K<=1000) the first solution is faster.]

This reminds me a classical quote:

“Everyone knows that debugging is twice as hard as writing a program in the first place. So if you’re as clever as you can be when you write it, how will you ever debug it?” – Brian Kernighan

Chinese stealing my code

This page was part of my last project which I developed in my company: Buff catalog.

And this page was part of the website of our client’s competitor in China, recently published: KranGear catalog.

The funny thing is that their javascript files even include MY comments, in Spanish.

The idea was to use a thermometer as the scroll bar, reflecting the different temperature protection of every product. The original page was developed around December 2006. I spent a couple of weeks tweaking the details, to get the smoothest result. I think it was an innovation, because the design department wanted to make it with Flash, and I showed them that this can be made with just javascript (including the zoom-in effect when you pass over the designs). And now a Chinese competitor copies me… hence I did a good job (even I could have done far better, but we were in a hurry to publish the website).

This reminds me of another UI effect, made with javascript, which I programmed for the Whisher home page (now they have a new website, but my work can be seen, without images, at webarchive’s capture). The idea was some kind of quick movement between slides, and I spent a couple of days creating the correct framework (divs, css, and javascript) to make it alive. Now it’s a quite common effect, that most javascript libraries have (like this example), but in those days it was new stuff.

Curious, because my most innovative work was in this area (javascript), when my position at the company was more server-side focused (php + mysql, mainly).

Stupid robots generating the Spanish Congress’ website

The thing to speak about this week in Spain is the end of the football league. But I don’t follow it!!

Let’s restart: the thing to speak about this week in Spain, regarding the “web world”, is the new website of the Spanish Congress, This new version was worth a bit less than 200 thousand euros. And it’s probably the worst HTML I’ve ever seen… just thousands of lines of tag soup. Just try to see the source code (right click in the page, and “View Source Code”)… even if you don’t know anything about HTML, you can see some really weird things, like a thousand of lines of CSS definitions that are stupid. It’s quite impossible to do it worse!

The truth is that it’s quite clear that some kind of robot has generated all this tag soup. Humans are not in this magnitude of stupidity. Finally, all the government’s websites must follow (by law) standards and usability guidelines: I guess they haven’t any idea of what this really means.

“Welcome to Spain”… What a shame!

“Tomato plants” sorting algorithm

Yesterday I was trying to program a Heapsort algorithm in C, just as a practice to remember how C was (as rough and pure as I remembered). It’s an interesting sorting algorithm, which doesn’t use extra memory, and has a good time complexity, O(n log n). In theory it is as good as the popular Quicksort, but it does have a weird behaviour with almost ordered lists (unsorting the items and resorting them later), using more time than Quicksort uses. This is the reason for its short popularity.

This behaviour has induced me to think about similarities between computer algorithms and nature algorithms. Some algorithms have somehow an inspiration from nature, like the Bubble sort which, as its name suggests, imitates bubbles inside a liquid. Others don’t reflect nature, and this maybe is not too good.

New plants growingI have a lot of tomato plants growing on my balcony. There are too many small plants. Some of them will grow quicker, stealing the soil’s energy from others. Some will not have enough energy to develop. Natural selection. Energy movements, randomness, and quicker first states remind me of something like Simulated annealing. It’s not a honest algorithm, meaning its result is not perfect, and maybe some items get better positions that they should. But it’s an interesting path to develop…

“Part of the inhumanity of the computer is that,
once it is competently programmed and working smoothly,
it is completely honest.”
Isaac Asimov

P.S: … or maybe I must keep my mind disconnected on Sunday morning, and spend the time watching some brilliant animated shorts.

Bandwidth thieves

Some people love using Google Images for finding images to use in their blogs. And they use the absolute URL, which means: they show the image in their sites, but they use YOUR bandwidth. In the last few weeks I’ve discovered quite a lot of webpages that use images located at my domain.

Some days ago I detected one, and I changed the content of one stolen image (a Dali’s clock) to another thing (a gay bears picture), and started laughing: the thief put the clock image beside a text which says something like “it’s time to change”… what a change! :-D.

But due to the quantity of thieves, I decided to create a script to “scare” them. It’s a small .htaccess:

Basically it verifies if the image is NOT referred from my site (or some usual RSS readers), redirecting the request to another place. The funny thing is that if the thieves use Google Images, they’ll see the correct image. But later, when they decide to add the absolute URL in their sites, the “change” takes place… (and sometimes they’ll not realize it because their browser will have cached the image, making more fun for the visitors).

Final tip: Don’t steal!!