Variable names

31.07.2008 16:52

Russell Coker gives some good reasons why you want to avoid using single character names for variables in your code.

I also occasionally find myself searching for uses of a variable i, only to find that half of the source code matches. A simple regular expression like \Wi\W usually works fine for me. But that already requires three times as many key presses as a fast / i search in vi.

The policy I try to stick to is to use single letter names only for loops that have an obvious purpose and are no longer than say ten lines or so. This usually removes the need to search for such variable names. Any variable that lives longer than that should have a descriptive name anyway. I'm pretty much against using index or similar generalized, verbose names for a simple loop counter. They don't tell you a lot more about the meaning than a single letter name and they make code harder to read. If it's a long loop or it's not obvious what it counts, then you should give it a proper name like pixel_cnt or something similar.

Russell also mentions that the most common loop counter names start with i because of the way variable types worked in FORTRAN. Interesting, because I usually start with n. I got used to that when I learned programming on Sinclair Spectrum. There the next BASIC keyword was entered by pressing the letter n. So n was most convenient as a for loop variable, because you could close the loop simply by hitting n twice. In nested loops letters near n had the priority, starting with m, b and so on.

Posted by Tomaž | Categories: Code | Comments »

Vanilla Skype

27.07.2008 16:14

This is old news (dated June 2006), but I just stumbled upon it and it may be interesting to some Skype users I know.

Vanilla Skype is a title of a talk given by Fabrice Desclaux and Kostya Kortchinsky at RECON 2006. You can get slides from the net (part 1, part 2) and they tell a pretty fascinating story about how far Skype will got to prevent you from messing with their software and their P2P network.

With the recent talks of backdoors in this particular piece of software, I can't help but think that with all that obfuscation and protection against reverse engineering they must have something to hide.

Posted by Tomaž | Categories: Code | Comments »

Dark magic and scantily clad virgins

26.07.2008 14:06

Last week I uploaded my first contribution to CPAN, the Comprehensive Perl Archive Network: Net::Zemanta, an object oriented Perl binding for Zemanta services.

This means that you can now start experimenting with Zemanta in your language of choice after a simple shell one-liner like this:

$ perl -MCPAN -e "install Net::Zemanta::Suggest"

Before operating the unit carefully read the user's manual and keep it for future reference:

$ perldoc Net::Zemanta::Suggest

For the impatient, here is an example that shows how easy it is to get a list of related articles for some text:

use Net::Zemanta::Suggest;

my $zemanta = Net::Zemanta::Suggest->new(
		APIKEY => 'your-API-key' 

my $suggestions = $zemanta->suggest(
		"Cozy lummox gives smart squid who asks for job pen."

for $article (@{$suggestions->{articles}}) { 
	print $article->{title}, "(", $article->{url}, ")\n";

Surprisingly uploading this module didn't involve much dark magic at all.

Posted by Tomaž | Categories: Code | Comments »

z80dasm 1.1.1

25.07.2008 21:08

I would like to notify users of z80dasm, my version of a disassembler for Zilog Z80, that I've released a new version. It contains a one-line change compared to version 1.1.0 that fixes a minor bug causing labels to be sometimes created without a good reason.

In detail, it was a typo in an if statement that was inherited from dz80, on which z80dasm is based. It caused every instruction that started with 0xDD or 0xFD to be recognized as a LD (nn),IX (only as far as label generation was concerned - actual instruction was decoded correctly). This created some random labels if bytes following the instruction decoded to a valid address within the binary block you were disassembling.

So, unless you're reverse engineering large chunks of code where extra labels may get in your way, there's no real hurry to upgrade.

Source and Debian packages are at the usual place.

Posted by Tomaž | Categories: Code | Comments »

They always win

15.07.2008 10:08

Yesterday at Zemanta headquarters, there was a discussion about the validity of some statistics. It went something like this:

They always win

(brought to you by the Creative Commons NC-SA 2.5 license of the original xkcd comic)

Posted by Tomaž | Categories: Life | Comments »

Computer is trying to tell you something

14.07.2008 19:02

I just saw this interesting tooltip when I moved the mouse over the System menu.

You need help

Posted by Tomaž | Categories: Life | Comments »

Wikimania 2008

12.07.2008 21:29

Wikimania 2008

I'll be spending the next week in Alexandria, at this year's Wikimania. I was at the first Wikimania in Frankfurt three years ago and it has been a pleasant experience (if I forget the desperate search for working wireless, but I guess that is mandatory at such events).

I wonder how much the conference has grown in these years. I remember Frankfurt as a nice, relatively small scale event. Halls were full of discussions about Wikipedia trivia and inside jokes (a lot of which I didn't get at that time) and Jimmy Wales was a kind of a celebrity everyone wanted to meet in person. I also recall the bad opinion I got about Richard Stallman from the way he answered questions at that conference. My impressions are still available at Cyberpipe's webpage by the way (Slovene text over English comment spam: day 1, day 2, day 3)

This year I'll be giving a 20 minute talk about Wikitag, the automatic link generation system I developed for Zemanta and a related lightning talk about an open-source MediaWiki dump preprocessor Wikiprep. So, if you're interested in the magic behind Zemanta's suggestion service and you'll be on that continent next week, you're kindly invited.

Posted by Tomaž | Categories: Life | Comments »

Tracing cobbles

08.07.2008 22:10

Back in 2004 I did some research into analysis of signals recorded by an instrumented tracer (English abstract). The problem I was working on involved this instrument in form of a metal sphere that you put into a river and let the water carry it just like a normal stone cobble. The tracer then measures and records the acceleration profiles of impacts with bedrock and other stones. The purpose is that from these measurements you can get some idea of what material there is in the river bed and what kind of erosion is going on there.

What I basically did is that I developed a pretty simple method of automatically recognizing the type of material that is hitting a tracer by looking at the output of three accelerometers.

To my surprise, dr. Matjaž Mikoš, one of my mentors, contacted me a few days ago with news that our work has been presented as a poster at the European Geosciences Union and the he held in invited talk about the topic at SDHR (Slovenian Association of Hydraulic Research).

Application of an Instrumented Tracer in an Abrasion Mill for Rock Abrasion Studies

(Click on the image for the full poster)

I'm really glad that the work I did turned out to be that useful.

Update: I've fixed the image so that now you can actually read the text on the poster. Thanks to Andraž for notifying me about that.

Posted by Tomaž | Categories: Life | Comments »

C constants

07.07.2008 21:02

Today I spent a couple of hours hunting down a bug in some of Zemanta's C++ code. I won't bore you with details, but I did learn something interesting about the way GCC works.

In essence the bug was due to a broken implementation of string comparison. Like this for example:

int cmp_strings(const char *x, const char *y)
	return x == y;

Now, if you know some basics you should see right away that this won't compare strings at all. What it will do is that it will compare the memory locations at which the strings are stored. But that's not the interesting part.

The interesting part is that a function like this had a couple of unit tests and they all passed with flying colors, while in real usage it broke horribly (as it should).

How was this possible? Consider the following program using the function above:

int main()
	printf("%d\n", cmp_strings("en", "en"));
	printf("%d\n", cmp_strings("sl", "en"));
	return 0;

Can you say what the output looks like without compiling it first?

Interestingly, at the first glance the output of this program supports the (wrong) theory that cmp_strings does in fact compare string content (and that's why the unit tests I mentioned passed).

What is really happening is that GCC is optimizing memory usage of the program and is merging equal constant strings. There is no use in storing the same constant "en" string three times in three different locations when one copy will do just fine (they are constant after all). So cmp_strings will work correctly for constant strings, but not for variable ones.

Oh, and -fno-merge-constants doesn't help with this, since it only affects merging of identical constants over multiple compilation units (on GCC 4.2.3 at least). In fact I see no way of disabling this optimization so that I could quickly check if any other code is also broken in this way.

Posted by Tomaž | Categories: Code | Comments »


01.07.2008 17:11

Today Slovenian highways switched from a modern contactless toll collection system called ABC to a piece of paper that you stick on your windshield and has to be manually checked by an army of inspectors.


ABC in all its tuned microstrip glory

ABC was a prepaid system where the amount you paid was proportional to the usage of the highways (well, to some approximation). The new sticky paper costs the same, no matter how much you actually drive. What is even funnier is that the speed limit through the tollgate is now 30 km/h while with ABC it was 40 km/h (and with the old system something actually had to be done while you drove through).

Welcome to back to 1950s.

This step back does have some bright sides though. One is increased privacy. Since the old system registered your tag on every entry and exit of the highway, it was possible for it to be used to track vehicles. An interesting step in the time when governments want to track citizensterrorists on every occasion. On the other hand you can already hear talks about Slovenia being the first country in Europe to adopt a new electronic system using satellite tracking, so this may not last long.

For people commuting daily by car like me the stickies also bring yearly toll expenses down to one tenth of what they were with ABC. Great for us, not so much for tourists just passing through our country and certainly not for Slovenian railways. And then people wonder why our public transport is in such a sorry state.

Posted by Tomaž | Categories: Life | Comments »