views:

802

answers:

8

Lion's Commentary on Unix Sixth Edition with Source Code is a wonderful book to learn how to read code. Reading code is important -- how does one learn how to write excellent code without having read excellent code?

But, sadly, while great writers, of fiction and non-fiction, all spend a great deal of time reading stuff, we, programmers, seem to avoid it like the plague.

Worse still, programming books usually go the same way. They might show a pattern or a style, but they often avoid showing good, complex code, and helping one go through it.

There are exceptions, of course. I hope.

So, with that in mind, what books are to be found which help one learn how to read code?

+8  A: 

"Beautiful Code" - Elegant examples in several languages. Learn what good code looks like. More over each chapter is written by great developers like K & R etc

alt text

"How Not to Program in C++ - Examples of code with bugs. Learn to recognize subtle problems by reading code.

alt text

The Practice of Programming by K&R - Gives an overview of code style.

alt text

Vinay
@Vinay: I think those are both good suggestions, so i restored your answer and added some explanatory text. Hope you don't mind!
Shog9
It certainly looks like a valid answer. I wonder if the books explain what the code is doing or not doing, or if they explain how to decompose and understand the code. Something like giving a fish vs teaching how to fish. Alas, they were both in my to-read list, so I'll get to them. :-)
Daniel
Somebody down voted. I thought it is irrelevant and so I had deleted them :)
Vinay
Darius Bacon
+1  A: 

I think that's an excellent question.

I think that the main problem is that there isn't a fixed form of code, so each code is its own artifact and may demand different strategies. In addition, a lot of academic research shows that people don't the same code alike, so it might not be possible to devise strategy.

The best recommendation I can give is a variety of research papers on inspection and reading, that helps you learn about mistakes and things that throw people off. However, nobody that I know of wrote a book on how to approach code.

Uri
A: 

As with reading fiction, it helps to read a wide variety of authors and topics. I recommend all of the O'Reilly books with titles like Programming [something], which tend to include serious code examples.

Peter Hilton
+10  A: 

How about this one Code Reading: The Open Source Perspective

alt text

I believe the author is also the Stack Overflow's member too. You probably can ask some questions to him.

Diomidis Spinellis

KOkon
+1  A: 

The best books for reading good code are the ones that have real code samples in them. Many books have partial, incomplete examples that only server to illustrate a specific point. That's fine for what it is, but it can lead to bad habits. I've found that the following books have excellent code samples.

You probably won't find books with code much better than the standard libraries for C and C++. These books are packed with real code used on real projects every day. The books are written by authors who wrote much of the code in the libraries themselves.

I like these three because each uses a different language to illustrate algorithms and data structures using real implementation code rather than the pseudocode you see in so many algorithms books.

If I were only allowed to pick one from this group it would be Programming Pearls. Each chapter was originally published as a magazine article, so the topics vary widely. What they all have in common are great code examples with great explanations by the author.

Also, it may go without saying, but it's probably better to read actual source code from a good open source project. When you read real code you get to see how a developer chooses to handle corner cases and exceptional conditions, something you rarely see in examples in books.

Bill the Lizard
Bill, source code is everywhere, sure, but that's learning by trial and error. While it can be effective, you can also fall into methodological traps (local maximums) you'll never leave. It's slow going too.
Daniel
A: 

I think "the best way to read code" doesn't exist.

I'd say that only for very small projects are you going to have the time to read all of the code. So for any real world example, you'll need some strategy to decide which part of the code is important. [as an aside: your brain is being bombarded with visual information all the time, but you only pay attention to (and process thoroughly) a small part of it; you want a good attention-like mechanism for information in the form of code]

The best strategy for deciding which part of the code to read depends very much on why you're reading it.

Say you're writing a window manager and need a good placement algorithm. It might be useful to find the window placement code in another window manager or two, study that code intently, and be inspired.

Say you've written a patch to a program, but introduced a bug in the process. Your reading is by necessity going to be spread out much more: you're looking for interactions between your new code and any preexisting code.

Say you're trying to get an architectural overview over some piece of software. Your reading will again be spread out, but even more than for finding the bug: you have no particular place in the code to "start from". Some visualisation tools might also be handy (static call graphs, class hierarchies, time and memory profiling hot spots in color).

That said, there may be ideas that can help you no matter why you're reading the code. I'm blank on that bit, sorry :-)

Jonas Kölker
+1  A: 

Some of these are difficult to find but are well worth tracking down:

Denis Hennessy
+1  A: 

You get used to it...I, I don't even see the code. All I see is...blond, brunette, red-head...

Seriously though, I dont think books help you learn to read code, practice and knowledge of the language being used helps you.

Mauro