Monday, November 22, 2010

Zero, One, Infinity

The three most useful numbers to programmers are zero, one, and infinity. Let's talk about why: It's the number of times something is used. That something can be a program, a block of code, or a data object.
Zero and one are pretty straightforward. Zero, it's not coming up at all. We're skipping it, because it's irrelevant, because we're low on resources, or because we just don't want it. If it comes up and we tried to skip it, it's a bug. One, we're doing this once. One copy of your business proposal. One copy of your personal finances. One copy of the pipe-flow model so the oil company can prevent blowouts in the new well. One copy of the game you're playing. It runs alone, and other things must be prevented from altering it. So far so good.
By infinity, we don't mean the mathematician's infinity. That would require literally forever to run if we ran it in serial, and an infinite number of CPUs and RAM chips if we ran it in parallel. No, by infinity, we mean arbitrarily many until we run out of resources. If you can run two, why not three? If you can run 3, why not 5? If you can run 5, why not 100? If you can run 100, why not 1,000,000? And something interesting comes up here.
A good example of the infinity model was back in the days when the significant computers were mainframes owned by an institution, and used by hundreds of users. They had text editors to take notes, program, and a myriad other things that store as words. The first attempt was to have a separate copy for each user, but that used too many resources. Programmers had to consider what could be shared, and what couldn't. Obviously, users should not see someone else's text stream, but the code to, say, draw up the text on the screen was invariant, and only one copy was necessary. So they all shared those parts of the code, and only one was in memory.
As an example in modern computing, take your media player. You probably have more than one media file on your computer, be it a movie, a song, or a MIDI sequence. It's quite rare that a media player requires your to load up the files one by one at the time you want to hear them, but more likely that it maintains a playlist. When you're tired of what's playing, you just press the 'next' button, and it goes to the next song in the list. The playlist is the infinity model, you can add songs to it until you run out of resources. Probably the hard drive fills up, and no more media can be obtained. It's not likely that your playlist grows so large that you run out of RAM to store it all, since the playlist is fairly compact, being only a pointing to the file name of the media, which it can load up quickly when indicated.

No comments:

Related Posts Plugin for WordPress, Blogger...