top | item 879101

Ask HN: What source code is worth studying?

125 points| idlewords | 16 years ago | reply

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?

114 comments

order
[+] sriramk|16 years ago|reply
I'm going to get downvoted for picking a non-open source option but...

The NT kernel is the most beautiful piece of code I've seen. Dave Cutler and team wrote some very, very elegant code that anyone (even if you're not a kernel hacker) can understand. If I need 'code inspiration', I spend some time looking through their code.

Now, the NT kernel itself doesn't have source out there but for folks who are students, you should be able to get your hands on the Windows Researh Kernel (http://www.microsoft.com/resources/sharedsource/windowsacade...). This has all the good stuff (and is much easier to build too)

[+] huhtenberg|16 years ago|reply
I spent several years writing drivers for NT and I don't know which part of the NT kernel you were looking at. The parts I saw were an utter mess lacking any sort of elegance whatsoever. If there were two ways to implement something, one simple and the other one contrived, you can bet your MCSD certificate NT will be using the latter.

The overall impression was that it was hacked together in the worst sense of the word. Like there were several teams developing it that did not communicate with each other. Pieces of the kernel are forced together rather than fit snugly by design. I cannot stress enough how mind-boggingly repulsive it was to work with it. And that's not even touching NDIS, the networking module, which can be used for scaring some serious shit out of young programmers.

If you put BSD or Linux code side by side with NT, look me in the eye and tell me that NT kernel is "the most beautiful piece of code you've seen", you will need to see a head doctor.

[+] slackenerny|16 years ago|reply
The NT kernel is the most beautiful piece of code I've seen

Modulo Plan9 kernel. It's also lot smaller a read (ca. 50 KLoC and that's it).

NT kernel itself doesn't have source out but for folks who are students

And folks who are hackers should be able to look in certain places to find the leaked version ;>

[+] dtf|16 years ago|reply
All the more heartbreaking that it was forced to don the hideous mantel of the Win32 API (itself further scarred from numerous assaults such as C2 compliance!)
[+] anatoly|16 years ago|reply
I agree that it's very beautiful code. I wouldn't go as far as "the most beautiful I've seen", personally, but it's really, really good. Also, when you come to it, you're typically shell-shocked from reading lots of crappy Win32 code, and it's just such a nice change.
[+] mahmud|16 years ago|reply
Your compiler's source code. That should shake your confidence in the world.
[+] pgbovine|16 years ago|reply
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.
[+] dtf|16 years ago|reply
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.
[+] wallflower|16 years ago|reply
"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/

[+] mbubb|16 years ago|reply
This is a good read and uses lots of examples from one of the BSDs (NetBSD as I rememeber).
[+] sgoraya|16 years ago|reply
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.

[+] thismat|16 years ago|reply
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.
[+] yesimahuman|16 years ago|reply
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.
[+] lpolovets|16 years ago|reply
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.
[+] mmc|16 years ago|reply
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/

[+] yan|16 years ago|reply
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.
[+] avinashv|16 years ago|reply
SQLite's source listing is great. I learned a lot about good C practices and documentation reading it.
[+] mahmud|16 years ago|reply
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/

[+] davidw|16 years ago|reply
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).
[+] jeremyw|16 years ago|reply
Especially its deep test architecture.
[+] geocar|16 years ago|reply
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.

[+] plinkplonk|16 years ago|reply
One way to find code worth reading is to select inspiring developers and look at their code. Most of the developers in "Coders At Work" for example, have Open Source code we can look at learn from.

The best code I've seen: -

Common Lisp - "Paradigms of Artificial Intelligence Programming" by Peter Norvig and "On Lisp" by Paul Graham

C - "C Interfaces and Implementations" and "LCC- a compiler for ANSI C" both by David Hanson. I also found the code for the Player/Stage robotic sim framework surprisingly readable.

I liked the Scala Actors library code as well.

If any HNers know any great codebases in Haskell or Erlang, please post here.

[+] mbowcock|16 years ago|reply
For haskell I would recommend Xmonad. The code is short, lots of interesting functionality including a well written plug-in system.

http://xmonad.org/

[+] jsyedidia|16 years ago|reply
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.
[+] nixme|16 years ago|reply
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...

[+] idlewords|16 years ago|reply
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.
[+] shabda|16 years ago|reply
Django's code is well written, and very well documented.
[+] axod|16 years ago|reply
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.
[+] revetkn|16 years ago|reply
If you're an iPhone developer, Joe Hewitt's three20 is the best open-source codebase I know of: http://github.com/joehewitt/three20
[+] boucher|16 years ago|reply
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.