Russell Coker: Perfect Code vs Quite Good Code
Some years ago I worked on a project where software reliability should have been a priority (managing data that was sometimes needed by the police, the fire brigade, and the ambulance service). Unfortunately the project had been tainted by a large consulting company that was a subsidiary of an accounting firm (I would never have expected accountants to know anything about programming and several large accounting firms have confirmed my expectations).
I was hired to help port the code from OS/2 1.2 to NT 4.0. The accounting firm had established a standard practice of never calling free() because “you might call free() on memory that was still being used”. This was a terribly bad idea at the best of times and on a 16 bit OS with memory being allocated in 64K chunks the problems were quite obvious to everyone who had any programming experience. The most amusing example of this was a function that allocated some memory and returned a pointer which was being called as if it returned a boolean, one function had a few dozen lines of code similar to if(allocate_some_memory()). I created a second function which called the first, free’d any memory which had been allocated and then returned a boolean.
Another serious problem with that project was the use of copy and paste coding. A section of code would perform a certain task and someone would need it elsewhere. Instead of making it a function and calling it from multiple places the code would be copied. Then one copy would be debugged or have new features added and the other copy wouldn’t. One classic example of this was a section of code that displayed an array of data points where each row would be in a colour that indicated it’s status. However setting a row to red would change the colour of all it’s columns, setting a row to blue would change all except the last, and changing it to green would change all but the second-last. The code in question had been copied and pasted to different sections with the colours hard-coded. Naturally I wrote a function to change the colour of a row and made it take the colour as a parameter, the program worked correctly and was smaller too. The next programmer who worked on that section of code would only need to make one change - instead of changing code in multiple places and maybe missing one.
Another example of the copy/paste coding was comparing time-stamps. Naturally using libc or OS routines for managing time stamps didn’t occur to them so they had a structure with fields for the year, month, day, hours, minutes, and seconds that was different from every other such structure that is in common use and had to write their own code to compare them, for further excitement some comparisons were only on date and some were on date and time. Many of these date comparisons were buggy and often there were two date comparisons in the same function which had different bugs. I created functions for comparing dates and the code suddenly became a lot easier to read, less buggy, and smaller.
I have just read an interesting post by Theodore Ts’o on whether perfect code exists [1]. While I understand both Theodore’s and Bryan’s points of view in this discussion I think that a more relevant issue for most programmers is how to create islands of reasonably good code in the swamp that is a typical software development project.
While it was impossible for any one person to turn around a badly broken software development project such as the one I describe, it is often possible to make some foundation code work well which gives other programmers a place to start when improving the code quality. Having the worst of the memory leaks fixed meant that memory use could be analysed to find other bugs and having good functions for comparing dates made the code more readable and thus programmers could understand what they were looking at. I don’t claim that my code was perfect, even given the limitations of the data structures that I was using there was certainly scope for improvement. But my code was solid, clean, commented, and accepted by all members of the team (so they would continue writing code in the same way). It might even have resulted in saving someone’s life as any system which provides data to the emergency services can potentially kill people if it malfunctions.
Projects based on free software tend not to be as badly run, but there are still some nasty over-grown systems based on free software where no-one seems able to debug them. I believe that the plan of starting with some library code and making it reasonably good (great code may be impossible for many reasons) and then trying to expand the sections of good code is a reasonable approach to many broken systems.
Of course the ideal situation would be to re-write such broken systems from scratch, but as that is often impossible rewriting a section at a time often gives reasonable results.
CHRISTOPHER MCKINLEY asked:
The German language has an expression that is "Zeichen der
Zeit". Literally translated this means "Signs of the time", the idea
behind that expression is that sometimes there are certains things or
events that are symptopmatic for a current development or a general change
of times.
One of these "Zeichen der Zeit" can currently be observed very
nicely in our neighbourhood. In the last half year three shops in our
direct vicinity closed down. One was a quite cute 60s cafe that was later
rented out to a citizens' action committee, the second was a shop that sold
wooden toys, the third was a health food shop. I don't know how long these
shops where around, but all of them had lasted for at least a couple years
if not longer. Now all three shops are rented out again, two are now temp
work agencies, the third is a "consumer bank" that specialises on small
consumer credits for things like cars, washing machines or entertainment
electronics. Apparently they also cooperate with some mail order firms.
Is this really where we want to go?
The
I'm glad to note that there's been a lot of movement over the last few weeks for the
This is a nice little bit of shell magic I discovered today:
Three weeks ago I was scanning in all our old analog pictures from the
I just discovered the recently-opened Velouria Espresso in Jamaica Plain
Three days ago I released KCheckGMail v0.5.7.3: nothing but a one-bug fix release.
Maj isn't really a big skating fan, but she seemed to have a good time this time around. Which made three of us. I think we've got the makings of a Sunday-afternoon family tradition. We'll see, at least.
tags:
This should really be titled A movie vaguely representing
Beowulf, but all sexed up with various salubrious elements .
Hrothgar was treated much better in the original; and all the
blatant and gratuitous sexuality brought in into the movie was a
turn off. But then, I might be in the minority of the audience who
had any familiarity with the poem.
The characters in the movie seemed two dimensional caricatures
(the only compelling performance was from Grendel s mother). And
the changes made to the story line also lost the prowling menace of
the latter years of the king of the Geats.
After watching Hollywood debacles like this one, I am driven to
wonder about why Hollywood writers seem to think they can so
improve upon the work of writers whose story has stood the test of
time. Making Beowulf into a boastful liar and cheat (even in the
tale of the sea monsters his men imply that that they knew their
lord was a liar) in an age where honor and battle prowess were
everything I mean, what were the producers thinking?
Most certainly not a movie I am going to recommend.
I had not researched the movie much before I went into the show,
and it was a surprise to me to see that this was an animated movie
a la Final Fantasy , and while I was impressed with the computer
graphics (reflections in general, and reflections of ripples in the
water were astounding), the not a cartoon but not a realistic movie
experience was a trifle distracting, and detracted from telling the
tale.
I like IMAX 3d, and the glasses are improving.
In a nutshell: If your system is kind of older than sarge (as in installation date, updates done in the mean time
don’t matter), beware of 2.6.23.x or update your grub boot sector, which Debian doesn’t do automatically on
package installation.
After 2.6.23.1 lasting for more than a month, I decided on wednesday to finally roll 2.6.23.1 out to most systems. Only
to be overrun by seven releases in a row (which even kernel.org infrastructure not properly handled). Ok, so it’s
2.6.23.8.
Installation of kernel on my test and semi-productive systems went just fine, so I took the opportunity of being in the
datacenter for some unrelated matter on a saturday to update two productive boxes. Just to find them not coming up
again. The first box, not connected to a KVM switch, but only to a serial console, looked like it simply died at the
place where grub usually says “Loading Linux...”, while the second box, on a “real” KVM switch,
was a little more helpful, saying “No setup signature found” (or something along this wording).
Since there was no internet on site with the boxes down (one being the main router, the second one being the full
service DNS resolver), I quickly chose the fall-back kernel and got back in business. Googling revealed that this is an
issue connected with old grub and kernel 2.6.23, and indeed, running grub-setup brought both systems back online. Had I
done this - as usual - from remote, this would have been an unexpected downtime of at least half an hour.
Since all systems in question have the same version of grub (the one from etch) installed, I proceeded to investigate
what has been going wrong here. Finding that grub does not have any maintainer scripts at all, it would have been my
responsiblity to update the grub code in the boot sectors to the new version after updating to etch. The package
didn’t bother doing this, and this is also not documented in README.Debian or NEWS.Debian. As this is a major
pitfall for systems that have been installed pre-sarge, and since updating the Distribution does not solve the issue, I
filed