Are Down With OPC?
Other People’s Code, or OPC as I’d like to call since I am a child of the 80′s, is defined by me as crappy code. Of course I was joking to some extent, but I believe it’s something we need to address.
I always hear about learning to writing good code, but hardly ever hear about learning to READ the code, both good and bad. I was so happy when my fellow teachmetocode.com expert Chuck interviewed JEG (that sounds so much cooler than James Edward Gray III) in his podcast where JEG spoke about reading OPC. If you haven’t checked out his podcast, please do it NOW by going to http://www.railscoach.com.
As a software architect, I read lots and lots of code. I saw some excellent code as well as what can only be described as something only the mother would love.
You see, the code is same as any writing. It tells you what the code is trying to achieve and tells a story. I would compare it to any other fine literature or a junk novel. It has personality and style. It also tells the story of what was happening at the time of writing. For example, when I see that unit tests are missing when the rest of code base isn’t, it’s a sign of something being pushed out in a hurry.
Having said all that, I’d like to share my method for reading code. The most of this will be “duh, no kidding”, but I think it’s a good refresher.
- Before sitting down to read the code, I get an understanding of the final outcome.
- Understand the convention, design pattern(s), and the opinion of the framework.
- Have the language reference, API docs, and any other reference material
- Identify the entry point. I came from C, leave me alone!
- Identify the prerequisites.
- Start reading
- As I read, try to put myself in the same shoes as the person writing the code. e.g. what is his thought patterns, why did he do this way, is this guy a beginner or expert, is he trying to say “fuck you” to anyone reading the code, why is he writing Java code in Ruby, and etc.
- Follow the steps and write them down on a writing pad
- Absorb and appreciate, even if I think it’s a pile of crap
Next time, I may actually create a screencast going over a code review if I can find the time. Let me know.