Reading Code

The two best ways of learning how to program are writing code and reading (good) code. I read a lot of code in my younger days, and I think it helped me. Programming courses give students plenty of practice in writing code. I wonder how many give students an opportunity to read good code. There should be a course in which all the students read and discuss a large program, like a literature class.

One problem with that idea is a lack of good code to read. Knuth’s TeX and Metafont books are good reading. But to really understand them you have to first wrap your head around TeX itself, which is a rather complicated macro processor. In some ways the code for TeX is easier to understand than TeX itself. Also, the code is in the Web version of Pascal, not a popular language today. The Lions book and TCP/IP Illustrated, Volume 2, show good coding, and are at least written in C, though again the code is rather old by today’s standards.

Are there any modern code bases that are helpful to read? It would be best if there were some commentary, or at least really excellent comments.


Posted

in

by

Tags:

Comments

8 responses to “Reading Code”

  1. jldugger Avatar
    jldugger

    How about glibc or BOOST?

    We read some stdlib functions in class, and BOOST seems like more of the same but with more documentation.

  2. cian Avatar
    cian

    The SEI at Carnegie-Mellon have a course based around reading code –

    “Reading Computer Programs: Instructor’s Guide and Exercises”
    – by Lionel Deimel and Fernando J. Naveda
    http://www.sei.cmu.edu/publications/documents/ems/90.em.003.html

    It’s interesting reading.

    Cian

  3. Lance R. Avatar
    Lance R.

    This is also getting a bit long in the tooth, but “A Retargetable C Compiler: Design and Implementation” by Fraser and Hanson is a good read and another example of Knuth’s “Literate Programming” presentation style.

  4. misham Avatar
    misham

    I agree with BOOST. Their code is a great example of how to write in C++ and the code stays way ahead of C Library.

    I would also recommend “Code Complete” by Steve Mcconnell (http://www.cc2e.com/) on general coding practices as well. It’s quite a read, but you can skip around and get what you need out of it.

    For C, I like to look at the Linux Kernel source. You’ll encounter all types of code from really bad to some brilliant implementations of various algorithms. The documentation is not as great as it could be, but there is a lot of community support and books on the code. You will also get code from very modern to really really old. You can look at support for “ancient” hardware to the latest wireless and embedded platforms.

    For Python, I like the Trac project by Edgewall (http://trac.edgewall.org/). They provide very good documentation and clear code.

    For Objective-C / Cocoa, Adium and Growl are good places to look. They use a lot of the frameworks Cocoa provides and are actively developed.

  5. Pádraig Brady Avatar

    “There should be a course in which all the students read and discuss a large program, like a literature class”

    Absolutely! This would be a brilliant way to learn.

    The coreutils “novel” might be appropriate.
    Each “chapter” is quite digestible and varied.

    http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=tree;f=src;

  6. ncm Avatar

    I don’t have any suggestions, but I will note the programming proverb, “Hell is other people’s code.”

    On a slightly different topic, I have encountered Google’s C++ coding style standard, and I can say without reservation that it has markedly reduced my perception of Google as an organization that values competence and taste. The only grace that comes to mind is that I have seen worse.

  7. Jonathan Avatar
    Jonathan

    “Three beautiful quicksorts” http://video.google.com/videoplay?docid=-1031789501179533828 addresses the issue and relates, IIRC, to this book http://oreilly.com/catalog/9780596510046/toc.html

  8. Ian Lance Taylor Avatar

    Thanks for all the great comments. Many of them are new to me. I’m especially glad to hear that there is a course.

    glibc has individual bits of good code, but as a whole is overly complex, with sparsely documented interdependencies between different parts.

Leave a Reply