tags:

views:

824

answers:

18
+10  Q: 

Good code to read?

Lurking around on Stack Overflow, I have seen a couple of comments like "reading code is better than reading codes" and "join open source projects and read other people's code, that will teach you". So I decided I would start doing that on weekends (read and analyze famous and tested code).

After watching Linus Torvalds talk about git I decided that reading git source code could be interesting.

Is there code that you think is worth reading? Why do you feel that way?

Edit: I didn't actually mention the language because I think I can read any kind of C, Java, PHP, Perl, Python, JavaScript, C# and a little bit of Lisp. I know the hard part will be understanding the algorithm and the decisions, not the grammar.

+1  A: 

See this related question.

Doug Currie
+6  A: 

I very recently started reading the source for Linux commands that I use all the time.

Bill the Lizard
+1  A: 

Actually I don’t think that reading C code is worth any while—unless you’re into kernel hacking and stuff. That’s my very subjective 2 cents only, though, and yes, I prefer managed languages. :)

Bombe
You have a very limited perspective.
dicroce
But i guess there must be some very crazy algorithms in git to search and analyze text inside a LOT of files in a efficient way?
DFectuoso
@DFectuoso, there may be some nuggets of wisdom hidden in there, I won’t doubt that. @dicroce, not really, no. I tried working on Git already, and frankly, I hate it when every project has to re-invent string handling because C doesn’t have any proper language support for strings.
Bombe
+1  A: 

I particularly like the source to TrueCrypt.

From the moment you unzip it and see the directory names, it's teaching you about its underlying structure. I had no experience with windows file system drivers, but within minutes I felt like I had a fairly decent understanding.

dicroce
+8  A: 

SQLite is small, extremely popular, high performance, builds on may platforms, has excellent test coverage, has many language bindings, and uses C and Tcl (for testing).

Oh, and it's public domain, so you can use the code any way you like.

Doug Currie
SQLite is amazing. Besides all the reasons you listed, it's also fairly well documented.
dicroce
This is is really tempting =)
DFectuoso
+4  A: 

I recommend reading Donald Knuth's book on literate programming or if you're really ambitious, reading part of his TeX source.

John D. Cook
+1  A: 

I personally find that reading through source-code is not enough. I suggest you read through some of the sources people here have suggested, and try and add another feature yourself. This will force you tackle the real issues which are rarely clear from just browsing through the source.

edit: As for sources to read, I highly recommend reading the Minix 3 code. It is very well documented and originated as an operating systems for learning operating systems.

Gilad Naor
+1  A: 

I'd say that there isn't much point in reading random code. Selecting which code to read is relevant to your learning needs. For example I would suggest to read NHibernate test project to get familiar with Unit testing.

Diadistis
+2  A: 

A boss I had once told me that it is far more important to be good at reading and understanding code than writing it (for your career). His argument for this was that you will nearly always be a member of a team, and no matter how prolific you are, the odds are good that the output of your team will dwarf your own, so to keep up, you need to be able to quickly grok what your teammates are doing.

dicroce
+1  A: 

I liked looking at the Quake II and III code.

scottm
Interesting... let's take a look
Ubersoldat
A: 

Java's Source Code is the best way to learn proper Java... except for Clonable. Oh! And Eclipse's source code taught me a lot about SWT.

Ubersoldat
+1  A: 

It's also worth reading Beautiful Code.

Brian Clapper
I was dissapointed with half that book. The section on regexps was very interesting though.
Marty
Not every chapter is a gem, but there are several that make the book worth the purchase price.
Brian Clapper
+3  A: 

Just reading random bits of code isn't going to get you very far.

Reading code only becomes truly useful when it's part of a bigger process - fixing a bug, implementing a new feature, optimizing an algorithm, etc.

True learning in software development is done by doing - writing, running, debugging.

You learn from mistakes and it's tough to make mistakes if you're not actually doing anything other than reading what someone else did :).

17 of 26
While it's generally good advice to get off one's butt and do things, this comment is like advising someone who wants to be a writer not to bother reading books except for some instrumental purpose. (Yes, to become a writer you must write. That wasn't the question. )
Darius Bacon
Indeed, i program 9 hours a day in my day job... so its not like i want to read a couple of pages and be the next big hacker... just want to get better at this by watching tested and used code
DFectuoso
Fair enough, the context/background of the situation was not entirely clear from the original post.
17 of 26
+4  A: 

Peter Norvig has some especially instructive code on his website and in his AI programming book.

E.g. world's longest palindrome, spelling corrector, solving Sudoku.

Darius Bacon
great find... i dont have more votes today but +1 +1 +1
DFectuoso
+2  A: 

C: Lua and SQLite.
Delphi: DeveloperExpress components (VCL editors and grids), VirtualTreeView

dmajkic
+2  A: 

Scott Hanselman does a weekly source code post where he picks something interesting he thinks developers should poke through.

I really advocate folks reading as much source as they can because you become a better writer by reading as much as writing. That's the whole point of the Weekly Source Code - reading code to be a better developer.

He comes at it from a .NET slant, but most of the code he picks is indeed pretty awesome.

Dan F
A: 

If you're interested in template programming and metaprogramming, reading the Boost code can be very instructive.

comingstorm