Why programmers write unneeded code

Programmers write a lot of code that is never used. There are numerous reasons for this. In Peter Seibel’s book Coders at Work, Peter Norvig gives his take on why this happens.

Seibel: Why is it so tempting to solve a problem we don’t really have?

Norvig: You want to be clever and you want closure; you want to complete something and move on to something else. I think people are built to only handle a certain amount of stuff and you want to say “This is completely done; I can put it out of my mind and then I can move on.”

Sometimes software developers believe there’s a high probability that some unrequested feature will be needed in the future. In general, they over-estimate such probabilities. The acronym YAGNI — you aren’t gonna need it — is meant to remind developers of this tendency.

It’s a great feeling to say “I’ve already done that” when someone asks for a new feature. Then you’re the hero, the sage who anticipated what needed to be developed. When you write code that’s not needed, perhaps nobody notices, and you can comfort yourself that the time is still coming when the world will want your feature. The times when you guessed correctly are more vivid in your mind than the times when you’ve been wrong and so you over-estimate just how often you’ve been right.

But sometimes it’s worthwhile to solve a bigger problem than you have to. It may make sense to create a more complete solution than is currently necessary while the problem is fresh on your mind; it will be harder to pick the problem back up in the future, so if you’re ever going to write it, now’s the time.

Norvig rightfully points out the down-side of seeking closure. Maybe the last 2% is intellectually satisfying but horribly difficult and not worth the effort. I’ve erred on both sides. Years ago I often erred on the side of developing functionality that was never used. Then reading Kent Beck convinced me that YAGNI is usually true. Since then I’ve erred on the side of wishing I’d done more while a project was fresh on my mind.

More programming posts

A 3,000 page proof

Mark Ronan wrote a popular book Symmetry and the Monster (ISBN 0192807234) about the story behind the classification of finite simple groups, or as Ronan calls such groups, the atoms of symmetry. All finite groups can be built up from simple groups somewhat like the way composite numbers are built up from prime numbers. Groups describe symmetries, and so the fundamental building blocks of groups are reasonably called atoms of symmetry. Ronan gave a lecture summarizing his book. Audio, video, and a transcript of his talk are all available here.

The classification of finite simple groups can be seen as a theorem whose proof is spread out over hundreds of articles and thousands of pages. A precise statement of the classification theorem is available here. One major piece of the puzzle is the theorem of Feit and Thompson. An entire journal issue was devoted to the 255-page proof of this one result. There is a project to simplify the proof, eliminating some of the redundancy between papers, etc. But it appears that the revised proof will still contain hundreds of pages of highly technical reasoning.

I enjoyed reading Ronan’s book a couple weeks ago. The biographical sketches in the book are the best part. The book begins with the study of symmetry via group theory, starting with the work of Évariste Galois and Sophus Lie. Someone with very little background in math could read most of the book. However, toward the end of the book when Ronan gets to the classification theorem and the role of “the monster,” he goes into more detail and there I believe he loses his audience. He goes into more detail than a non-mathematician would want to read, but not enough detail for a mathematician to understand exactly what he’s talking about.

I recommend starting with Mark Ronan’s lecture. If you want to go further, read the book, but feel free to skim over details toward the end.

(I haven’t seen a definitive count of the number of journal pages that comprise the classification proof. Ronan quotes one source who says the number of pages is “at least 3,000.” Other sources say “tens of thousands of pages.” Maybe it is unclear which papers should be included.)

Is programming getting easier or harder?

From Peter Seibel’s interview with Guy Steele in Coders at Work:

Seibel: It is easier to write software now because of advances we’ve made?

Steele: Well, it’s much easier now to write the kinds of programs we were trying to write 30 years ago. But I think our ambitions have grown tremendously. So I think programming is probably a more difficult activity than it was 30 years ago. … it’s not possible to understand everything that’s going on anymore.

Related posts

Circle of fifths and number theory

Why does music have a circle of fifths but no circle of thirds or circle of sixths?

If you start at on any note and go up by fifths, you’ll cycle through the entire chromatic scale. For example: C, G, D, A, E, B, F#, C#, G#, D#, A#, F, C. If you go up by fourths, you’ll get the same sequences of notes but in the reverse order. So there’s a cycle of fifths and a cycle of fourths, but there are no other ways to cycle through the chromatic scale other than the chromatic scale itself.

If you start at C and go up by minor thirds, for example, you’ll only hit four distinct notes before returning to where you started: C, D#, F#, A, C. You don’t cycle through all 12 notes, only four of them. Instead of filling out a chromatic scale, you fill out a diminished chord. You could fill out two other diminished chords by starting on C# or on D. Going up by major sixths produces the same sequence of notes as going down by minor thirds.

What’s special about fourths and fifths that their cycles cover the chromatic scale while cycles of other intervals partition the chromatic scale into smaller groups of notes? A fourth is 5 chromatic steps and a fifth is 7 chromatic steps. The numbers 5 and 7 are relatively prime to 12, that is, they share no factors with 12 (other than 1, which doesn’t count).

The numbers less than 12 and relatively prime to 12 are 1, 5, 7, and 11. These intervals correspond to the ascending chromatic scale, the circle of fourths, the circle of fifths, and the descending chromatic scale.

The numbers less than 12 and not relatively prime to 12 are 2, 3, 4, 6, 8, 9, and 10. Going up by 2 chromatic steps produces a whole-tone scale. Going up by 10 steps produces the same sequence of notes but in the opposite order. Going up by 3  or 9 steps produces a diminished chord. Going up by 4 or 8 steps produces an augmented chord. Going up by 6 steps produces a tritone pair. (I’m used to jazz terminology which uses the term “tritone.” Classical musicians would more likely say “augmented fourth” or “diminished fifth.”)

Now imagine a non-traditional scale that divided the octave into some number of parts other than 12. Suppose this new scale has n notes. Cycling in steps of size m will cover all n notes if and only if m and n are relatively prime. For example, if we divide the scale into 15 parts, we could cover all 15 pitches if we went up 4 steps at a time. We could play notes 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15.

If m and n are not relatively prime, let d be their greatest common divisor, the largest number that divides both m and n. Then going up d parts at a time will cycle through m/d notes and there will be d distinct cycles. For example, if there were 15 notes in our scale and we went up in intervals of 10 notes, we would cover 3 distinct notes, and we could make 5 different such three-note chords. For example, one such chord would be notes 1, 11, and 6, and another would be notes 2, 12, and 7.

If a scale had a prime number of notes, then every interval (other than an octave) would cycle through all notes.

Why is the 12-note scale so common? There have been other systems, but these are mostly subsets (at least approximately) of the 12-note scale. The answer seems to have something to do with the fact that intervals in the 12-tone scale have simple frequency ratios. For example, a fifth is a ratio 3:2 and a forth is a ratio 3:4. (More on that here.) These intervals are pleasant to our ears. There was a prehistoric flute in the news a few weeks ago and it appears to have been based on the same musical intervals common in modern music.

Related posts