(Ln(x))3

The everyday blog of Richard Bartle.

RSS feeds: v0.91; v1.0 (RDF); v2.0.

Previous entry.


11:23am on Monday, 2nd January, 2006:

Hacking Code

Anecdote

I did some more programming of QBlog this morning, to re-enable to automatic update of the web site (so it'll insert cd ../2005 where necessary).

Aw, man, what a hack. I'd be ashamed to let another programmer see it.

It all started off swimmingly well. I observed good programming practices the whole way. I was very pleased with the QBlog software to start with, and would have showed it to anybody. Not any more, though.

The rot set in when I moved from the private web space I have at the university to youhaventlived.com for space reasons. While I was at it, I moved all the files to a directory system based on the year, because Unix can get picky when directories get lots of files in them. As I had to do the programming quickly, I took a few shortcuts. Hmm...

In general, it's good practice to program efficiently even if it takes you longer to program something than will ever be saved in speed of execution. However, sometimes speed of programming takes precedence, and this was the case back in October and yesterday/today. The resulting code works (hence a successful hack) but ho boy is it ugly. It's not quite as ugly as the code for MUD2, which was beautiful until it went through a Pascal-to-C transliteration program that uglified it. It's still bad, though.

Example: I cut-and-pasted code. This is not something a programmer should ever do: if code is similar enough to be cut-and-pasted, it should be made into a separate routine. That way, if you need to make a change to the cut-and-pasted code, you only have to do it in one place. I did it, though, because it was quicker.

Example: I character-counted. QBlog filenames are all composed of the word QBlog followed by 6-character date specifier followed by a succession letter followed by .html. The filename of this post you are reading, for example, is QBlog020106A.html. I define QBlog as a constant, PRE, and .html as another constant, EXT, which is good. If I want to find what year a file is in, as I would if, say, I wanted to know what directory to look for it in, I would examine the 10th and 11th characters, which, as C uses 0-indexing for strings, would be fs[9] and fs[10]. Except, really it should be fs[strlen(PRE)+4] and fs[strlen(PRE)+5]. In fact, to be really proper I should define 4 and 5 to be YEARTEN and YEARUNIT respectively.

It gets worse. In a couple of places, I set some global variables to trick a routine into thinking it's doing something it wouldn't do ordinarily, instead of adding a parameter to the routine instead. In another case, I have one routine that ought to be two separate routines, as its only two calls use non-intersecting sets of parameters (one uses the first three and ignores the second three; the other does the reverse). It would only take half an hour to fix, but it's so bleah that I don't want to do it.

Worst of all, though, is that I make assumptions. In particular, I assume that any reference in a QBlog entry to another year's posts will start ../. This is correct — they do all start like that — but they're not the only things that start like that. Every QBlog entry refers to the latest entries and archived entries files, which are not then followed by a year specifier such as in ../2005. However, if I check that the 4th character is a 2 then this is fine (at least for this millennium). The thing is, I shouldn't be checking for a 2 because the files should never reach the stage where they need to be checked — augh, trust me, it's anathema to a programmer's sensibilities.

Still, at least it works.

I just hope the people who program nuclear power stations don't go similarly sloppy when facing a deadline...


Latest entries.

Archived entries.

About this blog.

Copyright © 2006 Richard Bartle (richard@mud.co.uk).