The Beauty of Programming
By Linus Torvalds (creator of Linux)
I don鈥檛 know how to really explain my fascination with programming, but I鈥檒l try. To somebody who does it, it鈥檚 the most interesting thing in the world. It鈥檚 a game much more involved than chess, a game where you can make up your own rules and where the end result is whatever you can make of it.
And yet, to the outside, it looks like the most boring thing on Earth.
Part of the initial excitement in programming is easy to explain: just the fact that when you tell the computer to do something, it will do it. Unerringly. Forever. Without a complaint.
And that鈥檚 interesting in itself.
But blind obedience on its own, while initially fascinating, obviously does not make for a very likeable companion. What makes programming so engaging is that, while you can make the computer do what you want, you have to figure out how.
I鈥檓 personally convinced that computer science has a lot in common with physics. Both are about how the world works at a rather fundamental level. The difference, of course, is that while in physics you鈥檙e supposed to figure out how the world is made up, in computer science you create the world. Within the confines of the computer, you鈥檙e the creator. You get to ultimately control everything that happens. If you鈥檙e good enough, you can be God. On a small scale.
And I鈥檝e probably offended roughly half the population on Earth by saying so.
But it鈥檚 true. You get to create your own world, and the only thing that limits what you can do are the capabilities of the machine and, more and more often these days, your own abilities.
Think of a treehouse. You can build a treehouse that is functional and has a trapdoor and is stable. But everybody knows the difference between a treehouse that is simply solidly built and one that is beautiful, that takes creative advantage of the tree. It鈥檚 a matter of combining art and engineering. This is one of the reasons programming can be so captivating and rewarding. The functionality often is second to being interesting, being pretty, or being shocking.
It is an exercise in creativity.
The thing that drew me into programming in the first place was the process of just figuring out how the computer worked. One of the biggest joys was learning that computers are like mathematics: You get to make up your own world with its own rules. In physics, you鈥檙e constrained by existing rules. But in math, as in programming, anything goes as long as it鈥檚 self-consistent. Mathematics doesn鈥檛 have to be constrained by any external logic, but it must be logical in and of itself. As any mathematician knows, you literally can have a set of mathematical equations in which three plus three equals two. You can do anything you want to do, in fact, but as you add complexity, you have to be careful not to create something that is inconsistent within the world you鈥檝e created. For that world to be beautiful, it can鈥檛 contain any flaws. That鈥檚 how programming works.
One of the reasons people have become so enamored with computers is that they enable you to experience new worlds you can create, and to learn what鈥檚 possible. In mathematics you can engage in mental gymnastics about what might be. For example, when most people think of geometry, they think of Euclidian geometry. But the computer has helped people visualize different geometries, ones that are not at all Euclidian. With computers, you can take these made-up worlds and actually see what they look like. Remember the Mandelbrot set戮the fractal images based on Benoit Mandelbrot鈥檚 equations? These were visual representations of a purely mathematical world that could never have been visualized before computers. Mandelbrot just made up these arbitrary rules about this world that doesn鈥檛 exist, and that has no relevance to reality, but it turned out they created fascinating patterns. With computers and programming you can build new worlds and sometimes patterns are truly beautiful.
Most of the time you鈥檙e not doing that. You鈥檙e simply writing a program to do a certain task. In that case, you鈥檙e not creating a new world but you are solving a problem within the world of the computer. The problem gets solved by thinking about it. And only a certain kind of person is able to sit and stare at a screen and just think things through. Only a dweeby, geeky person like me.
The operating system is the basis for everything else that will happen in the machine. And creating one is the ultimate challenge. When you create an operating system, you鈥檙e creating the world in which all programs running the computer live戮basically, you鈥檙e making up the rules of what鈥檚 acceptable and can be done and what can鈥檛 be done. Every program does that, but the operating system is the most basic. It鈥檚 like creating the constitution of the land that you鈥檙e creating, and all other programs running on the computer are just common laws.
Sometimes the laws don鈥檛 make sense. But sense is what you strive for. You want to be able to look at the solution and realize that you came to the right answer in the right way.
Remember the person in school who always got the right answer? That person did it much more quickly that everybody else, and did it because he or she didn鈥檛 try to. That person didn鈥檛 learn how the problem was supposed to be done but, instead, just thought about the problem the right way. And once you heard the answer, it made perfect sense.
The same is true in computers. You can do something the brute force way, the stupid, grind-the-problem-down-until-it鈥檚-not-a-problem-anymore way, or you can find the right approach and suddenly the problem just goes away. You look at the problem another way, and you have this epiphany: It was only a problem because you were looking at it the wrong way.
Probably the greatest example of this is not from computing but from mathematics. The story goes that the great German mathematician Carl Friedrich Gauss was in school and his teacher was bored, so to keep the students preoccupied he instructed them to add up all the numbers between 1 and 100. The teacher expected the young people to take all day doing that. But the budding mathematician came back five minutes later with the correct answer: 5,050. The solution is not to actually add up all the numbers, because that would be frustrating and stupid. What he discovered was that by adding 1 and 100 you get 101. Then by adding 2 and 99 you get 101. Then 3 and 98 is 101. So 50 and 51 is 101. In a matter of seconds he noticed that it鈥檚 50 pairs of 101, so the answer is 5,050.
Maybe the story is apocryphal, but the point is clear: A great mathematician doesn鈥檛 solve a problem the long and boring way because he sees what the real pattern is behind the question, and applies that pattern to find the answer in a much better way. The same is definitely true in computer science, too. Sure, you can just write a program that calculates the sum. On today鈥檚 computers that would be a snap. But a great programmer would know what the answer is simply by being clever. He would know to write a beautiful program that attacks the problem in a new way that, in the end, is the right way.
It鈥檚 still hard to explain what can be so fascinating about beating your head against the wall for three days, not knowing how to solve something the better way, the beautiful way. But once you find that way, it鈥檚 the greatest feeling in the world.
***
Reprinted with permission from Just for Fun: The Story of an Accidental Revolutionary, by Linus Torvalds (creator of Linux) and David Diamond, HarperCollins, 2001.