Mat Noguchi turned me on to a talk by James Coplien. The purple book ("Advanced Styles and Idioms in C++") used to be my bible, so I checked it out - one of the things that struck me besides his "How do you know computer science isn't a science? Because it has 'science' in the name." joke was him talking about beauty and asking "How many of you have opened a source file and said 'that is some beautiful code'?"
Damn that's rare. Usually I start working on a legacy project and I react with horror. I've seen some API's I consider beautiful - the dreamcast rendering API and XNA Game Studio (if you ignore the Xact stuff) - but code? One thing that comes to mind is when we went from Tony Hawk 1 to Tony Hawk 2, Neversoft overhauled their 2D rendering and that was beautiful, though I'm not sure I would have appreciated it if I hadn't seen how it was before.
Producers hate to hear from their coders about beautiful code. "Is that why it took so long?" they'll ask. And who can blame them, because the next coder that comes along and looks at that 'beautiful' code will probably think it's hideous.
But maybe, like Christopher Alexander says, we can find some area of agreement. Maybe most coders find most of the same things beautiful.
My Question To You: what is some code you consider beautiful? It has to be code you didn't write yourself. And preferably open source, so we can all take a look at it.
Here's the talk, btw:
I recently dug into the FCollada code to import some collada files into the engine i was testing. I was quite surprised by how nice it was. As you said in comparison to how much bad code i've seen. Given the lack of any agreement on a standard set of 3d classes in my experience. This might be a decent choice.
http://www.feelingsoftware.com/content/view/62/76
Posted by: Menace | July 30, 2009 at 11:28 AM
I can't think of any particular open source project that qualifies, but I can tell you criteria that are necessary (but not sufficient).
It needs to be well formatted, and consistently so, so that I can follow the program flow without having to read every symbol.
It needs to be commented correctly. By that, I mean header comments for each function that document the API, and any side effects, and basically no comments inside functions, that prevent you from being able to follow the program flow. If some piece of code is tricky enough that it needs commenting, there better be a pretty darn good reason to not just rewrite the code to be clearer. Also, comments should actually match the code.
Functions should be short enough that you're no more than a couple of page-ups to where any local variable it uses is declared.
Basically what I'm saying is that beautiful code is code that goes out of its way to facilitate me following it when I'm not familiar with the contents, either because I didn't write it, or because I wrote it so long ago I don't remember it.
Posted by: Skip | July 30, 2009 at 11:30 AM
I know it's not gamedev related, but the ASP.NET MVC source code can be seen online here:
http://aspnet.codeplex.com/SourceControl/BrowseLatest
for webdevs, it is beautiful code
Posted by: Joel Martinez | July 30, 2009 at 08:19 PM
This public domain indie game library called "Pixie": http://www.tophatarcade.com/dev/pixie/
I was pleased whenever I opened the source to look at anything. I don't agree with the code formatting, but the code itself is easy to read and understand - and more importantly - to extend.
Posted by: Aaron | October 03, 2009 at 04:07 PM
I must admit I ussualy take advantage of open-source software without even checking the source files; but every now and then I have to check some files for errors (errors ussualy coming from my side) and honestly, I either feel like I'm coding something terribly wrong, either they're coding something terribly wrong. The sad part is that they ussualy don't, I am.
Still, I can remember a great open source project that made me think that's a beautifull code, and the best part is that it's a game.
I'm talking about "Hero of Allacrost". Not sure if I can add links here but you can Google it :) That's beautifull code really.
Chers!
Posted by: Claudiu | October 13, 2009 at 12:14 AM