Monday, April 20, 2009

Careful what you ask me!

Some poor guy wrote to me to ask me what I meant by the following line from the little 'bio' paragraph I used to put at the end of programming articles:

[Greg is] a devout believer in the religious idea that, when a computer program works, it's a complete coincidence.

I sometimes get questions about articles I wrote (many years ago), but no one had asked me about this line, so I was pleased to respond. But the poor guy didn't know what he was getting into, because I just went on and on and on:

> I haven't thought of it before. and I know it may sound silly, but I'm
> curious to know why you think so..

Not silly at all! I've gotten many emails about my articles over the
years, but no one asked about this most-important of topics.

In programming, we use so many methods to try to control the
complexity, but in the end, I, at least, feel like I don't have a
handle on the whole system, and when something works, it *feels* like
I just got lucky.

When I first wrote that bio, I was just a year out from writing
DOS/Windows games. It drove me nuts -- one bug and you have to reboot
your computer. It really frustrated and traumatized me, so that I
would be crossing my fingers every time I ran the program. I guess I
needed more reliability than that, which is why I was quite pleased to
switch to Java.

If I think about the enormous complexity of the whole system, it
boggles my mind. I write this code running inside emacs, an ancient,
huge program that, itself, contains a lisp interpreter; my code is
compiled by a huge program that itself was compiled by a huge program
that, itself, was in turn compiled by a huge program, going all the
way back over the years to someone who wrote a tiny assembler and
punched it into the front panel of some primitive computer.
Meanwhile, the compiler is reading from disk spinning at who knows how
many RPM, with a magnetic head floating just above the fragile
surface. The disk sectors are being read, possibly out of order, into
memory buffers, probably inside an interrupt where some special pin on
the chip halted the processor in the middle of decoding some zany
machine language that looks like

0a 62 6f 67 67 6c 65 73 20 6d 79 20 6d 69 6e 64
2e 20 20 49 20 77 72 69 74 65 20 74 68 69 73 20
63 6f 64 65 20 72 75 6e 6e 69 6e 67 20 69 6e 73
69 64 65 20 65 6d 61 63 73 2c 20 61 6e 20 61 6e
63 69 65 6e 74 2c 0a 68 75 67 65 20 70 72 6f 67
72 61 6d 20 74 68 61 74 2c 20 69 74 73 65 6c 66
2c 20 63 6f 6e 74 61 69 6e 73 20 61 20 6c 69 73
70 20 69 6e 74 65 72 70 72 65 74 65 72 3b 20 6d
79 20 63 6f 64 65 20 69 73 0a 63 6f 6d 70 69 6c
65 64 20 62 79 20 61 20 68 75 67 65 20 70 72 6f
67 72 61 6d 20 74 68 61 74 20 69 74 73 65 6c 66

but also, since it's DOS (or Windows 3.1, same thing), the memory is
being paged around by some crazy 'dos extender' which has all its
interrupts going; all of this data is being parsed by a crazy state
machine with hundreds of states, and turned into a code tree and
analyzed and rendered into more '79 20 63 6f 64' and written to an
executable file with interlocking code sections; and then that file is
read into memory which does the run-time linking, relocating pointers
or whatever the hell it does, and don't forget that we might be
triggering page faults and reading from and writing from disk, and all
of this is written in a language that is almost certainly not Java,
all of this is running inside a little chip of silicon, tiny currents
running through tiny wires, right next to humming power supplies and
spinning fans, and any single component might go bad and a 0 changes
to 1 and you'll never never ever ever know...

I'm telling you, I get freaked out thinking about it, and when my
program *doesn't* print "Segmentation Fault: core dumped", it seems
like I just got *really lucky*.

So that's why.

In the morning, my code feels to me like it's a nicely-designed
bicycle, with a few well-understood moving parts, and I have to adjust
this or that and get riding; by the end of the day, it seems more like
an off-shore oil rig slowly toppling into the sea...

Put another way, in the morning it feels like science, and in the
evening it feels like religion.

No comments: