Ask HN: What source code is worth studying?

125 points by idlewords ↗ HN
I realized recently that it's been a long time since I looked at anyone else's code outside of the context of debugging or working on it for hire. In your opinion, what are some examples of particularly well-designed or implemented software projects worth looking at to broaden one's own horizons as a programmer?

116 comments

[ 4.3 ms ] story [ 203 ms ] thread
SQLite's source listing is great. I learned a lot about good C practices and documentation reading it.
If you like that style of C code, Tcl's is similar (and it's not a coincidence, as Dr. Hipp is part of the Tcl core team).
Thanks for the tip, I didn't know that. I haven't ever used Tcl besides some tinkering a few years ago, whereas SQLite is used incredibly often, but I'll read any C that's well written.
You will enjoy "C Interfaces and Implementations", along with the full, literate source-code for LCC.

This is also good, Standard Function Library:

http://legacy.imatix.com/html/sfl/

Where can one find the full literate source code for LCC? I haven't been able to find it anwyhere, and god knows I searched.
There's no literate source linked to on that page. Just plain, normal, barely-commented C.
"There's no literate source linked to on that page. Just plain, normal, barely-commented C."

The book by David Hanson is the literate program.

Ok, so the literate source, in a form that I can actually play with, isn't acutally available. It doesn't even seem to be maintained in literate form in SVN.

That's what I was trying to determine.

If you're interested in literate programming: TeX
This is half answer/half tangent, but I like http://www.google.com/codesearch a lot for browsing and reading code. A fun thing to do is to look at how others implement something you want to implement, or use some library you want to use. You end up finding a lot of different ways of doing the same thing, and if you find something that you want to explore more, it's easy to browse around.
The Linux kernel: http://miller.cs.wm.edu/
Equally or more valuable than trying and sit down to read the kernel source is to follow the patches and discussions thereof on the linux-kernel mailing list: http://lkml.org/

I've found that doing so a) improves my knowledge of what's going on under the hood tremendously, and b) has taught me a fair bit about how to do code reviews and make difficult design decisions. Studying the kernel itself line-by-line would take ages, but reading a few threads a week depending on what piques my interest is totally manageable.

I don't treat is a novel, but a reference. I'll often wonder how certain things are implemented, and I'll poke around for the data structures and algorithms until I gain some understanding of how it works.
I highly recommend the Stanford GraphBase, written by Donald Knuth. It's C code written by Knuth using the literate programming tool CWEB. If you don't know about CWEB, it's not hard to learn how to use, and probably is already installed on your system if you have TeX. The Stanford GraphBase is available for free download on Knuth's web-site, and also comes bound in a nice paperback book, that was reprinted in 2009. Knuth also makes available many other programs written using CWEB on his web-site, but I would start with the GraphBase.
Django's code is well written, and very well documented.
I think you tend to learn more studying badly written badly documented code. Certainly there's a lot of knowledge to be gained being able to decipher spaghetti code with no documentation. I guess it depends on your aims though.
Any recommendations for php? I wrote a php/mysql web application for SaaS customers. I am looking at ways to improve performance/cache/error handling or simply write a better code.
Perhaps MediaWiki?
Personally, I wouldn't recommend it. Unfortunately I can't give a better alternative, but I've done some hacking on MediaWiki since version 1.7 and while it's some of the better examples of PHP out there, it still leaves lots to be desired in different places.
Thanks. My post was just a guess.
I learnt a lot by using frameworks as symfony. The initial learning curve is steep, but then you can understand architecture better. The jump to other languages such as python and java was really easy after that.
The symfony framework and Doctrine ORM are both well written projects.
For a smallish project that you can figure out fairly easily, I recommend the Kohana framework. It's extremely well documented and it's quite easy to read. Many people actually recommend reading its source to supplement the documentation and after having to do so myself I can see why.
For systems programming, I've learned a lot from reading the source for open solaris: http://src.opensolaris.org/source/

And the LLVM compiler is far more understandable than gcc sources: http://llvm.org/viewvc/llvm-project/llvm/

I agree with this, and along the same thought, OpenBSD (or any BSDs really, but OpenBSD tends to favor simpler implementations) source is very clear and concise.
Minix, not being as 'real world' as the BSD's or Linux, is even more readable, if you're just interested in a quick glance at how an OS works.
From Minix' site: MINIX 1 and 2 were intended as teaching tools; MINIX 3 adds the new goal of being usable as a serious system on resource-limited and embedded computers and for applications requiring high reliability.

Is Minix 3 still readable, or do you recommend Minix 2?

Good question. My experience was with Minix 2: I needed a floppy driver to weld on to eCos, and Minix's was by far the easiest to deal with. I haven't looked at Minix 3.
Minix3 has comments galore, but seems plagued by one/two letter variable names like 'c' and 'ip'
Your compiler's source code. That should shake your confidence in the world.
for an easier time, your favorite language interpreter's source code ... interpreters are easier to comprehend than compilers, since you can 'follow along' with the flow of execution in the main interpreter loop just like you're executing a program ... e.g., creating stack frames, allocating heap memory, assigning variables, etc.
Squeak Smalltalk. For Java, if you can get hold of it, the old Acme Webserver. That was very clean code.
Squeak is pretty sweet. I was looking at the Kernel category and the Parser classes. Pretty clean stuff.
This really depends on the language. Ruby's code is (or at least was) quite nice. It is pretty much OO written in plain C.

By contrast Perl makes very heavy use of macros for portability, to an extent that may induce brain lock in many people, and makes your debugger very hard to follow. This is not to say that its use of macros is a bad thing in the end, but it is a definite shock.

Yeah, after hacking with perl for a while, i got interested in how it worked under the hood, and had some trouble following.

Check out the regex compilation module: http://cpansearch.perl.org/src/GBARR/perl5.005_03/regcomp.c

And execution: http://cpansearch.perl.org/src/GBARR/perl5.005_03/regexec.c

Well, anything touched by Ilya Zakharevich is going to be hard to read, even by the standards of the Perl source code. That means that the regular expression engine will be particularly hard to read.
I haven't perused it myself, but every time I see this question posed, someone always mentions Lua: http://www.lua.org/ftp/

Similar previous discussions:

http://news.ycombinator.com/item?id=225577

http://www.reddit.com/r/programming/comments/26dyh/ask_reddi...

Thanks for these links - I figured the topic must have come up before, but couldn't Google my way to the right HN thread. Hopefully there will be a search box on this site one day.
"Code reading requires its own set of skills, and the ability to determine which technique to use when is crucial. In this indispensable book, Diomidis Spinellis uses more than 600 real-world examples to show you how to identify good (and bad) code: how to read it, what to look for, and how to use this knowledge to improve your own code."

http://www.spinellis.gr/codereading/

This is a good read and uses lots of examples from one of the BSDs (NetBSD as I rememeber).
Any recommendations for good C# code? Much of the web is open source oriented so I don't hear too much about great C# code.
There are some code examples on Eric Lippert's blog that are made of good C# code.
Mono's c# compiler is written in c#. SharpDevelop is another good choice.
One project I found fascinating reading a while back when I was working on graphics, is AGG. It's a vector graphics toolkit, a bit like Cairo, Quartz 2D or Java 2D. It uses a particular style of C++ - a mixture of template programming together with regular polymorphism - to build up a collection of rendering components. The components are almost Unix-like - you plug them together to build your customized rendering pipeline, and the C++ compiler's template logic takes care of making everything hardwired (or dynamic, if you choose to use the polymorphic components):

  http://www.antigrain.com/
The end result is a toolkit which is at a lower level than Quartz 2D et al, but could easily be used to build such an API. It can also be adapted for embedded, or high precision uses.
http://www.haiku-os.org/documents/dev/painter_and_how_agg_wo...

http://bit.ly/131cxL (.doc file)

those help. if you can figure out the source code without the documentation at the agg site + those files, more power to you. but basically you are nuts.

Thanks for the links, I've never seen those before. I read the agg code without the docs, but it would have been difficult without the example programs. The docs definitely help when looking at some of the algorithms though. I remember spending a good day or two staring at the anti-aliasing code, together with the freetype rasterizer (on which it was based), trying to figure out what the hell was going on. Then in just clicked.
If you're an iPhone developer, Joe Hewitt's three20 is the best open-source codebase I know of: http://github.com/joehewitt/three20
Three20 is really useful. It's also quite powerful. But I wouldn't recommend it to someone trying to learn/study idiomatic Cocoa.

Joe tends to do things his own way, which works out fine, but does tend to depart from most people's Cocoa/Touch code.

I hear good things about the webkit source code
Any recommendations for Ruby/Rails projects?
I'm a big fan of ThoughtBot's stuff (http://www.thoughtbot.com/projects). Mostly I use Shoulda and FactoryGirl, but I've been consistently impressed with their ideas and code quality.

For idiomatic Ruby and Rails stuff I have probably learned more from Rick Olson's stuff (http://github.com/technoweenie/) than any other single source. Sometimes I think he tends to be too clever for his own good, but the code is good to read for that reason even if simpler things are better in production.

Read the source to Marcel Molina's AWS::S3.
Quake source code: www.idsoftware.com/business/techdownloads/

From a graphics and game engine perspective, it was very informative to go through pieces of of the source - I was mainly interested in the client/server and collision detection areas of the code.

I'm still pretty wet behind the ears, but looking through Quake source (and quake 2), was quite the experience. I though the code was beautiful, as well as impressed with how DRY everything seemed to be....oh, and variable naming, small things like that, really impressed me.
I second this. It's cool that the ideas behind some of the more simple things in the Quake game (such as the console, bindings, commands, etc.), are still being used as the back bone of other products (see: Valve). They are implemented very elegantly in the Q1 source.
Qmail: http://cr.yp.to/qmail.html

It feels well organized almost throughout, and it's a joy to experience the clarity of thought that went into it. In fact, most of djb's code has a similar feeling to it.

Arthur Whitney's code: http://www.nsl.com/papers/origins.htm

Being able to read this (and not merely decode it, but read it, in a manner similar to how you would read a book) will broaden your horizons like nothing else I've seen.

I learned about sockets and networking by looking at the source for wget.

That's also where I saw calloc.c - A portable implementation of calloc. I distinctly remember it being one of the cleverest hacks I've seen.