Beautiful code is code that inspires awe or admiration in programmers. Here are a few ways in which code can be beautiful.

1. It reads like english. (Or your native human language.) Well chosen variable, function, method, class, typedef names can make a line of code readable to even a non-programmer. This is beautiful, and usually requires no commenting. Ex:

volume = length * width * height;

// or:

if ($theUser->canRead($thisPage)) { 
   showPage($thisPage);
} else { 
   showError("You don't have read permission!"); 
}

2. Code that takes a rather complicated problem, and solves it with a few, easy to understand lines of code. Ex:

unsigned int fib(unsigned int n) {
   if (n > 1) return fib(n-1) + fib(n-2);
   else return 1;
}
Note that the solution is horribly inefficient, and anyone who uses this in a production environment should be shot. However, its beauty is in its simplicity.

3. Reusable code. When I see code that can be used in many useful ways, it's exciting. Computers are a tool to allow us to complete tasks more quickly. If I don't have to rewrite code, I can complete my task more quickly. Thus, reusable code is beautiful. I once had the joy of writing a large piece of very reusable code for work. However, it's long, and probably owned by my employer, so I'll forego the example.

I'd be interested to find what other geeks think of as beautiful code. Are there certain qualitites you look for in beautiful code? Do you have a favorite bit of beautiful code?

Log in or registerto write something here or to contact authors.