views:

1669

answers:

34

I am looking for books that present interesting software system at the source code level. One such book which I loved is Building Problem Solvers. It presents a series of truth-maintenance systems written in Common Lisp. The book reads like an insightful and well-presented commentary of the code. Can you recommend any other books in this vein?

+13  A: 

Your question pretty much describes Beautiful Code. Each chapter is by a different developer, describing what project or piece of code they thought was really well done. It has a very wide variety of example code.

Bill the Lizard
Not a bad book, but with surprisingly little actual code, given the theme. Many of the recommendations here have a *lot* more code per page on average, still of high quality.
Darius Bacon
Yeah, some of the developers selected to edit each chapter went on at length describing the code. Mostly, their insights are worth it.
Bill the Lizard
+5  A: 

The GoF Design Patterns book has some interesting sourcecode in SmallTalk and C++, and lots of good explanation.

FlySwat
+4  A: 

The TeX and Metafont books by Donald E. Knuth are classic examples of that. They introduced literate programming, based on DEKs observation that it makes sense to write programs as if they are supposed to be read and understood by people instead of compilers. The TeX and Metafont systems were written in WEB, which could be translated to TeX for printing and pascal for compiling.

Later versions of WEB are cweb and noweb. The book about the retargetable c compiler lcc was written in noweb:

A Retargetable C Compiler: Design and Implementation (Paperback) by David R. Hanson (Author), Christopher W. Fraser (Author)

Stephan Eggermont
Actually, the lcc compiler was written using Norman Ramsey's noweb (http://www.cs.tufts.edu/~nr/noweb/).
Peter S. Housel
Thank you, will change
Stephan Eggermont
+2  A: 

SICP, The Little Schemer, The Seasoned Schemer

nmiranda
+2  A: 

Software Tools by Brian W. Kernighan and P. J. Plauger

It should be required reading for all programmers.

Bryan Oakley
+1  A: 

"Programming in Haskell" by Graham Hutton is a great book, easy on the eyes and mind. He's a nice way of explaining Haskell. I've learned big times by reading it.

http://www.cs.nott.ac.uk/~gmh/book.html

Johannes Schaub - litb
+3  A: 
jamesh
That's a great, great book. It's not so much in the vein of Building Problem Solvers, however: much less code in the book, and that's pseudocode. The incomplete supplementary code on the web does make this a relevant answer.
Darius Bacon
+6  A: 
jamesh
Yeah, one of my favorite books.
Bill the Lizard
A: 

Code Complete has great examples of both good and bad code.

I also found Code Craft to be full of excellent examples.

JosephStyons
A: 
Jim C
+7  A: 

Paradigms of AI Programming by Norvig. It's rather like Building Problem Solvers: a variety of interesting AI programs, well explained, developed realistically in stages, illustrating larger themes, also in Common Lisp, and with good advice on realistic engineering concerns; but the code is, to my taste, far better. It's also worth reading if you're not into Lisp or AI. Hell, it's one of the great works of 20th-century literature.

Darius Bacon
Thanks! I love this book already.
namin
+3  A: 

Fraser and Hanson, A Retargetable C Compiler: Design and Implementation. Full description with most of the source code, in literate form, for a self-hosting ANSI C compiler. Tasteful design and code. C is not my favorite programming language, and the bulk of the code goes to implementing all the complications of it, detracting from the interest of the book, but it does what it does very nicely.

Darius Bacon
+2  A: 

Goldberg and Robson, Smalltalk-80: The Language and its Implementation. Includes fully-worked out examples of some substantial little programs, including a Smalltalk virtual machine written in Smalltalk. (That part is online in HTML, as well as the full-book PDF linked above.) (They don't include the code for the compiler and all the libraries, I should note.)

Darius Bacon
+4  A: 

Tanenbaum's Operating Systems Design and Implementation has a complete source code listing of the Minix kernel with documentation describing how it works. It is intended to be used to teach an operating systems course. Architectural Flamewars aside, it is quite a nice instructive guide and will teach you quite a lot.

In an Operating systems paper I did once we had to implement a device driver for Minix. This was quite a good learning experience.

ConcernedOfTunbridgeWells
+1  A: 

Friedman, Wand, and Haynes, Essentials of Programming Languages. Many interpreters and other language processors (like a compiler, a CPS converter, etc.) for a series of small programming languages, developed to illustrate and explain the principles. The first edition is more fun and hacker-friendly, IMHO, than the second (I haven't read the third, newly out this year). Written in Scheme. Great, essential stuff if you're interested in programming languages.

Darius Bacon
+2  A: 

James F. Gimpel, Algorithms in Snobol4. An overlooked classic with lots of fun code in a terribly obsolete programming language. Emphasis on string processing. Hard to find. The code is available online but probably hard to get much benefit from without the book.

Darius Bacon
+1  A: 

Paul Graham, On Lisp. Fine examples of the power of Lisp. The best chapters on macros I've seen anywhere (though I haven't read Peter Seibel's Practical Common Lisp, which is said to be good). His programming style is not so much to my taste (favoring too-abbreviated global names, anaphoric macros, etc.) but still interesting and educational.

Darius Bacon
+3  A: 

Lisp in Small Pieces by Queinnec. The best book available on the implementation of Lisp; a fair amount of overlap with Essentials of Programming Languages, but more advanced in the area it concentrates on. (His interpreters and compilers deal with more real-world details than theirs, which were properly focused on teaching the essentials.) Prose and code style are less elegant than many of my other recommendations. Oh, it uses Scheme, not Common Lisp.

Anatomy of Lisp by John Allen covers much of the same material, with excellent old-style Lisp code, but is, well, older.

Darius Bacon
+2  A: 

Java Puzzlers by Joshua Bloch and Neal Gafter

Each section contains source code that contains interesting and insightful tricks and edge-cases; the code is discussed in detail, and one grows more knowledgeable about computing in general (although some of the gotcha's may be Java-specific) as one reads the book.

David
+5  A: 
Darius Bacon
+3  A: 

Donald Knuth, Literate Programming. Has a few extended examples doing neat things.

Knuth and Levy, The CWEB System of Structured Documentation. Only mildly interesting because CWEB is pretty complicated but does a fairly standard sort of job.

Knuth, The Stanford GraphBase. Knuth, MMIXware. Lots of literate programs; I haven't read these.

Knuth, TeX: the Program. Knuth, Metafont: the Program. These look neat but I have not read them either. Perhaps the biggest single literate programs published in books.

Darius Bacon
+3  A: 

Niklaus Wirth and Gutnecht, Project Oberon: The Design of an Operating System and Compiler. Full source code, with good explanations, of a beautiful, innovative operating system, including the UI.

Darius Bacon
+2  A: 

Chris Okasaki, Purely Functional Data Structures. Just what it says, with code in Haskell and ML. Not your parent's data-structures book.

Darius Bacon
+1  A: 

Edsger Dijkstra, A Discipline of Programming. Lots of small but nontrivial examples of developing a program and its proof of correctness hand-in-hand.

Darius Bacon
+1  A: 

Richard O'Keefe, The Craft of Prolog. A bit of a grab-bag but a tasteful exposition of ideas and examples relevant to functional programming as well as Prolog. O'Keefe is entertainingly opinionated, not unlike Snape at Hogwarts when a student screws up.

Leon Sterling (editor), The Practice of Prolog. A collection of articles, each an extended example, with code, of making Prolog do something interesting, usually something AI-ish. Chapters by O'Keefe, Chris Mellish, Sterling, others.

Darius Bacon
+1  A: 

Artificial Intelligence Programming by Charniak, Riesbeck, McDermott, and Meehan. Also comparable to Building Problem Solvers (the AI/Lisp world had a real tradition of excellent books packed with advanced, interesting code, didn't it? Pity about that past tense there). First edition was written in a dead Lisp dialect; second in Common Lisp. (IIRC the second edition dropped some nice stuff, though, in favor of CLOS.)

Darius Bacon
A: 

Writing efficent programs. Sit down with a beer and enjoy

EvilTeach
+2  A: 

Brian Harvey, Computer Science Logo Style, in three volumes. Aimed at children and teens, but well-done and builds up to fairly advanced material.

Abelson and diSessa, Turtle Geometry explores math with code. Much more advanced than Harvey; unless you have a math degree already you'll learn a lot. Unlike Harvey this is more on the math ideas than the programming (but the key theme is the synergy of math and programming to explore ideas).

Darius Bacon
+1  A: 

The Lions book lists the Unix v6 kernel source, with commentary. Of historical interest but doesn't motivate one to plow through it in detail (or at least not me). Its original, unpublished form was in two volumes so you could have both the code and the commentary open at once; the published book makes you flip back and forth way too much (unless you splurge for two copies). What's cool about this code is how low-tech and brute-force it feels. ("When in doubt, use brute force.") Their dialect of C really was like a high-level assembler; people say that of C today, but it was much more true then, and in their style of coding.

Edit: the link works in preview, but not in the post, so here it is again: http://en.wikipedia.org/wiki/Lions'_Commentary_on_UNIX_6th_Edition,_with_Source_Code . I suppose the comma or the quote broke the formatting.

Darius Bacon
+2  A: 

P.J. Plauger, The Standard C Library. A full, portable, clean, reasonably efficient implementation of the standard library, with discussion of the design tradeoffs. Includes the relevant pages from the standard. (C89, not C99.)

David Hanson, C Interfaces and Implementations. A bunch of other libraries in C, also with extensive design and implementation discussions. Uses literate programming.

Darius Bacon
+1  A: 

Programming Collective Intelligence

The code is available online as well

sundeep
+2  A: 

Mark Jason Dominus, Higher-Order Perl. Ideas from the functional-programming world brought to Perl. The longest example is a constraint-based domain-specific language for diagram drawing.

Darius Bacon
+2  A: 

Timothy Budd, A Little Smalltalk. Design and implementation of a small self-contained Smalltalk system in C and Smalltalk. (VM, compiler, and library.)

Darius Bacon
A: 
mas