Posted on

Code to Read

A few days ago, Richard and I were talking about code to read. I think that you can learn a lot as a programmer by simply reading the code of others, in much the same way writers improve their craft by reading the works of their peers. So the obvious question, then, is what to read? There is obviously a lot of shit code out there, and if you decide to read some bad code with the intent of honing your own skills, it may very well have the opposite effect.

So below are some of my thoughts on code that is worth reading, broken up by language. I have restricted this to languages we use here at Cyber Sprocket, but I urge everyone to contribute anything useful they can think of, regardless of whether or not it’s written in a language we use.

JavaScript

The source code for my browser of choice, Conkeror (http://conkeror.org), is fantastic. It is based on the same XULRunner platform that is the foundation for Firefox, and our own ColorFish application. The code for Conkeror has no hesitations about using the more esoteric, non-standard features of XULRunner’s JavaScript support. I’m talking about things like using `get` and `set` to create getters and setters, and `let` within arbitrary blocks to create closures over variable bindings.

The Conkeror code can show you some cool ways to take advantage of Mozilla’s JavaScript engine, which can benefit you directly at Cyber Sprocket. Not because we always have the luxury of *exclusively* targeting Firefox, but Conkeror can also show you things you can’t do if you want to be cross- platform.

Python

If you want to learn some advanced Python, like how to use meta classes in a real-world situation, then look at the code for Django. Specifically the code for django.db.models.ModelBase. It can really show you the differences between things like __new__() versus __init__() for creating classes. Keep in mind that Django uses Python 2, and there are notable differences in Python 3. Right now I don’t know of a killer app in Python 3 that I could recommend to you.

PHP

Grab the code for PHPUnit (http://www.phpunit.de) and check it out. It is a fantastic example of well-structure PHP code. The gritty details of the style are not the best, which is to say that it breaks a number of our internal standards. But the logic of PHPUnit is very sound. And on top of that, it really makes good use of PHP’s object system by taking full advantage of things like standard library interfaces (e.g. `Countable`).

And besides, how many PHP applications do you see use type hinting in function declarations? Really? PHPUnit is great through-and-through. Also—we should use PHPUnit for unit testing our PHP applications, in my opinion. But that’s another discussion for some other time, like a standards meeting.