Reading source code is a good way to improve as a programmer, but I've never seen a great explanation of how to do it. We often read textbooks & novels linearly, perhaps taking notes along the way. What do you do when trying to understand how a program works?
- Try the user-facing version of the program with your own inputs
- Read the API
- Trace through the core functions in your head, transforming imaginary inputs
- Start at "main" and fire off the debugger!
I read the Ruby on Rails source code when I studied the MVC pattern. Many discussions leave out critical parts of MVC as done in Rails:
- The web server negotiating the communication with the browser (the controller's HTML output has to get to the browser somehow)
- The dispatcher translating the incoming HTTP request to function parameters and instantiating the controller (you don't want this logic in the web server!)
When people say MVC they really mean "MVC + the subsystem to run it". Since I'd used Rails for a while before digging in, I had an idea of what to expect but this was nevertheless enlightening. But if I had never touched Rails (or MVC) and used it for a few projects I wouldn't be sure where to start.
I'm curious: How do you go about understanding the source for a project?
Edit: Thanks for the comments! To clarify my question: "They say the best way to write well is to read. How do you read literature with the goal of improving as a writer?" The analogous question is:
- How do you read acknowledged "well-written code" with the intent of improving as a programmer?
With Rails, reading the code demonstrates several techniques (such as "Convention over configuration" -- put files in named directories so the program can automatically find them) and design patterns (MVC) that make the code clean, functional, and elegant. How do you go about extracting these & other lessons from code to improve your own skill?