Compile-time checked exceptions

May 2nd, 2007

C++ offers an ability to specify which exceptions might be thrown by a given function or method. Unfortunately because of its terrible implementation (the exception specification is only checked at runtime) it is rarely used. I (and many others) think that this would have been much more useful as a compile time feature.

Here is how I think it could work. Every function has an exception specification which can be explicit or implicit. If it is implicit it is calculated by creating the union of the exception specifications of all called functions and methods and the set of exceptions the function actually throws itself, and removing from that set any of the exceptions that that function actually catches. That way, exception specifications are optional but are used if they are there.

Then, certain functions (like the main() function, functions callable from C code, destructors and functions implemented in C code) can have an implicit exception specification of the empty set, eliminating an entire class of bugs.

Negative-overhead exceptions

May 1st, 2007

There are two schools of thought about how to handle errors in computer programs. By "errors" here I mean things that are out of the programmer's control, like "out of memory" or "file not found" rather than mistakes in the actual program (which is a whole other topic). Ideally we want programs which don't just crash when they hit such unexpected circumstances but rather tell the user exactly what the problem is and give them the opportunity to fix it and try again or do something different instead.

The first school of thought is that functions which fail should return some kind of "status code" (which may be "success" or one of several other values indicating different types of error). This is how most of the functions comprising the Windows API signal failure.

The problems with status codes are:

  1. Whenever you call a function you need to check it's status code or risk missing an error (which usually makes things worse). It's tedious and error prone (mistake prone) to write the code to do this and sometimes programmers forget.
  2. The information about an error you can return is quite limited - you can say "file not found" but you can't say which file wasn't found or what the program was trying to do at the time (you have to determine this from context).
  3. If you're using the return value of your function for a status code you can't use it for something more natural. For example, if you're writing a square root function that needs to return an error code when given a input that is negative, it can't also return the answer in a natural way.

The second school of thought is that failures should be indicated by "throwing an exception", which is technically rather more complicated. This involves looking at the function that called the failing function, and the function that called that one and so up the stack until you find a function that says "hey, I know about that exception - pass control to me and I'll deal with it". This eliminates the need for all the functions in between to know about the possible failure at all (other than cleaning up their resources).

Exceptions are new and spiffy and are generally considered to be the "right way" of doing things in new programs. Unfortunately, exceptions also have drawbacks:

  1. Your program is less "explicit" about what it actually does - certain controls paths are "hidden away" in the exception mechanism. Proponents of exceptions argue that this is a good thing.
  2. Certain programming styles cannot be used in the presence for exceptions. For example "do X, then do Y, then do Z, then do W if Y failed." Proponents of exceptions argue that these styles are mistake prone (it's all to easy to forget to do Z) and should be avoided anyway.
  3. While many programming languages in widespread use support exceptions, not everything does. In particular, certain "glue" for interfacing between different bits of code written in different languages generally doesn't support exceptions because they need to be compatible with languages which don't support exceptions (the old "lowest common denominator" problem).

Another criticism often made of exceptions is that they are slow. This may have been true in the past, but in practice it turns out that exceptions are no slower than status codes in the success case (they are slower in the error case, but that doesn't matter too much because errors should be pretty rare).

I think that it is possible to make programs that use exceptions even faster than programs that use status codes. With the right set of tables, the error case code can be completely separated from the normal case code. No code that deals with or tests for errors even needs to be loaded into memory until an exception is actually thrown. Then control passes to a "throw" function which loads the tables and error handlers into from disk and examines the stack to determine which functions the exception passes through and which function will actually handle the exception. The code is faster than the "status code" version because it doesn't have to have all those "if failed" tests sprinkled throughout.

I haven't tried implementing it, but as far as I can tell there are very few places where this scheme would have any overhead at all. One is this situation - State0::RunState() and State1::RunState() can't be folded together with my scheme because they have different stack unwinding information.

The other limitation is that the stack layout and set of constructed objects has to be completely determined by the instruction pointer in any given function. This is usually the case but does mean that functions can't allocate dynamically sized arrays on the stack. These don't tend to be used in practice because they have the serious problem that it is very easy to overflow the stack. If you do know in advance a maximum possible size for this array you may as well just allocate the maximum size.

The little scientist

April 30th, 2007

I have told Gennie that I don't mind what Alexander does when he grows up as long as he grows up to be a scientist. I don't mean that that necessarily has to be his job title or that he has to wear a lab coat, just that I hope that he always uses science and the scientific method in whatever he does. I hope he continues to take a keen interest in the world around him, develops theories about the world around him and then does experiments to find out if those theories are true.

I do this in my job (and non-work life) all the time. Actually I suspect everyone does it, but most people probably don't realize that they are actually doing science when they (for example) try another channel when there's static on the TV to find out if it's a problem with the TV (or cable provider) or just a problem with one particuar channel.

Maybe I should have said "I don't mind what my child does for a living as long as he understands what science is and appreciates its value whenever he uses it". But that's less catchy as a T-shirt slogan.

Robot squirrel

April 29th, 2007

When I first arrived at Microsoft I had a few days to settle in before I actually started work. I took the opportunity on one of these days to wander through the campus so I would know where to go on my first day, and to just explore a little. At one point on my walk I noticed a squirrel sitting on top of a fencepost staring it me, its eyes following me as I walked. I wondered (not seriously) if it was a robotic squirrel invented by the research department.

Musical toy

April 28th, 2007

The new and more logical musical stave I proposed yesterday could also form the basis for a fun musical toy. It would consist of a set of bars of various lengths representing various intervals, with the length proportional to the logarithm of the frequency multiplier (so maybe 15cm for an octave, 8.8cm for a fifth, 4.8cm for a major third and so on) and a set of connectors into which one or more of these bars can be plugged.

Each of the connectors would have a button on it which, when pressed, makes a sound. One of the connectors has its frequency fixed at (say) 440Hz (or maybe is tunable with a potentiometer) - the frequencies of the other connectors depend on how far away they are from the fixed connector (which in turn depends on which interval bars are plugged in. The connectors are built in such a way that if two bars are plugged in to opposite sides, their ends touch.

The result would be a sort of "build it yourself piano keyboard" which would help children (and adults) learn about musical intervals and tuning systems.

The mathematics underlying music

April 28th, 2007

Did you know that almost all Western music is based on an approximation? 524288 = 531441 to be precise (which is true to an accuracy of about 1.3%). These numbers are quite special:
524288 = 212
531441 = 319
Their ratio is called the Pythagorean comma and is the difference between 12 perfect fifths and 7 octaves. In Western music this ratio is treated identically to 1 (i.e. if you go up a fifth 12 times you'll end up on the note you started with, 7 octaves up). This approximation caused an enormous amount of trouble for early piano tuners.

Making this sort of approximation is quite important otherwise there would be an infinite number of notes (if you keep going up in intervals of fifths and going down in intervals of octaves you never get to exactly where you started).

But if we're using computers instead of more primitive instruments to make music, having an infinite number of possible notes doesn't really matter - the computer can represent them as exactly or approximately as you like, making it possible to do perfect just intonation and use any intervals you like without approximating.

The really interesting thing about thinking about intervals instead of "the 12 notes" is (for me) not so much that the different sounds are possible but that the underlying harmonic structure of the music is made explicit. The musical stave with its lines, spaces, sharps and flats has evolved rather than being designed and as such seems to hide the underlying intervals rather than revealing them. A fifth, for example, can be represented by 2 units, 2 units plus an accidental or 2 and half units plus an accidental depending on the note and key signature. But when the music is played they all sound like the same interval.

A much more logical stave would have time going down the vertical axis and frequency along the horizontal axis. Vertical lines would represent notes of constant pitch and horizontal lines would represent particular intervals. The horizontal scale should be made logarithmic so that a particular interval would be the same length no matter what its absolute pitch is. Intervals would be marked with their ratio to make it clearer what they really mean (though with practice it would probably be possible to read this kind of stave by eye without the interval markings). So if an octave is represented by 30mm, a perfect fifth (marked \frac{3}{2}) would be represented by 17.5mm (\displaystyle 30\frac{\log \frac{3}{2}}{\log 2}) and a major third (\frac{5}{4}) by 9.7mm (\displaystyle 30\frac{\log \frac{5}{4}}{\log 2}).

I ought to try drawing various pieces of music like this to see how it looks. It would also be much easier to write music using "weird" intervals like 7/4 and 12/11 using this stave.

Multiple scrollbars

April 26th, 2007

Ever have a window which has so much text in it that the vertical scrollbar moves too coarsely to allow you to find the position you want? Like you have an entire dictionary loaded as a text file and you're trying to find the word "madrigal". You get to "machine" and (after some fiddling with the mouse) manage to move the scroller one pixel down only to find that you have overshot all the way to "magical". Then you have to scroll all the way back up to madrigal or go back to machine and scroll down. Either of which, depending on the size of the dictionary, may take quite some time. Most annoying.

Wouldn't it be good if, for long documents like this, you had two scrollbars side-by-side. These would work like the hour hand and minute hand on a clock. The rightmost one would set the position in the document coarsely and the leftmost one would be for fine-tuning it. Once you had got to the "early Ms" with the rightmost scrollbar you could quickly get to any nearby word with the leftmost one.

For best distribution of precision between the scrollbars, the region of the document corresponding to the entirety of the leftmost scrollbar (X) would be to the entirety of the document as the window would be to X (i.e. the pixel size of the two scrollers would be the same).

The scrollbars would be set up so that moving the scroller of the leftmost scrollbar one pixel scrolled the document by at most one line. If the document is too large, than a new scrollbar is added - particularly long documents might have 3 or even more scrollbars, each with the same virtual document size to virtual window size ratio.

Lensing editor

April 25th, 2007

One problem I'm always wrestling with while programming is how to see as much as possible of my program on screen at once. I tend to use a very high screen resolution (1600x1200 usually) and a very small font in my text editor (6x8) to see a large quantity of text at once. My coworkers despair that they can never read what's on my screen and say that I must have very good eyesight (I do at short distance - reading road signs while driving is another matter). But even 150 lines is not enough sometimes.

A while ago I had an idea for effectively increasing the number of lines even further. Vertically "compress" the lines that are further away from the cursor, while showing the closer lines at normal size. The further lines wouldn't be legible (unless the document is quite small) but you can see the overall "shape" of the document and easily navigate to different sections. The effect would be kind of like squashing the document vertically so that the whole thing fits on the screen and then looking at part of it with a horizontal bar magnifier centred on the cursor (although it would be a magnifier with an extended cross section so that even lines of text quite far away from the center were magnified a little bit, and there would be no obvious boundary between magnified and non-magnified text). Because of the similarity to a bar magnifier, and by analogy with the concept of a "folding editor" I call this idea the "lensing editor".

Sonic head tracking

April 24th, 2007

Here's an interesting idea I had a while ago. It involves using the microphone in a headset like this, a fixed set of speakers and a high-resolution sound card (like almost any modern sound card) to track the position of the head of the person wearing the headset.

The software works by sending sounds through the speakers and trying to pick them up through the microphone. Once it manages to synchronize the sent pulses with the received pulses it can calculate the exact delay from sending a pulse to receiving it. Part of this delay will be due to the hardware involved or the latencies in the operating system, but these delays should be fixed. The remainder of the time is caused by the finite speed of sound in the medium between the speaker and the microphone. To a fairly good approximation this speed will be constant (about 300m/s) so using the delay one can work out the distance between the microphone and the speaker. Do this for two speakers and you can triangulate the position to a circle in the plane normal to the line between the speakers. Two more speakers and you can determine the position of the user's head quite precisely, up to the resolution limit of the soundcard (most modern soundcards can do 192k samples/second, giving a positioning accuracy of about 1.6mm).

Why would you want to do this? Well, the position of the user's head is a particularly interesting datum because it can be used to move windows around giving a pseudo-3D view. Suppose you have two windows on the screen and one is partially covering another. Normally to see the hidden part of the lower window you have to use the mouse (or worse, the keyboard) to move the upper window or bring the lower window to the foreground. But with a pseudo-3D view each window has a "Z-position" in front of or behind the plane of the monitor. If the user's head moves left then the closer windows move right and the further windows move left, allowing you to "see around" the upper window by moving your head, just as you would do with real objects. I haven't tried it yet, but I think it would be quite effective, and a cheap way to make a user interface much more immersive. I thought of doing this years ago but with an infra-red gadget strapped to the forehead - it only recently occurred to me that this could be done less intrusively with the microphone that many people wear anyway for telephony purposes.

Play high

April 23rd, 2007

I think one of the best things about having a big part in a play is the high that you get after a particularly successful performance. I always feel so "on" in that situation - extra confident and witty. For example, after one rehearsal of Cash on Delivery we were trying on the costumes for the first time. One of the other characters was wearing an absolutely hideous dress and I made the old joke "It's very becoming. Though I'm not sure quite what it's becoming..." which seemed quite out of character for me. Maybe I was just starting to feel comfortable in the presence of my fellow cast members or maybe it was a natural high caused by the adrenaline of the situation. Almost like being drunk, but with no hangover in the morning.