You learn to swim by swimming. Similarly, you learn to read code by reading code.
Every programmer writes code that the computer understands. The far more difficult task is to write code that people can understand.
Reading code can be hard because you trying to internalize computer instructions into human instructions. The only information you have are the instructions and from that you need to find out the intention of the programmer who wrote it.
Most of programming is actually just reading.
It’s good to practice reading code because it builds your confidence.
Everything on a computer happens for a reason - the code instructed it to. If we want to understand the reason, we need to understand the code.
If there is a problem - one of the first steps is to read over your code.
If you train your skills, you’ll learn how to spot syntax errors really quickly.
To become a great programmer, Read Great Programs. - Peter Norvig
The best way to do this is just start reading code.
When you do this exercise, think of yourself as an anthropologist, trucking through a new land with just barely enough of the local language to get around and survive. Except, of course, that you will actually get out alive because the internet isn’t a jungle. - Zed Shaw in Learn Python the Hard Way.
You are trying to internalize something that isn’t yours - so your mind will try to reject it - which is why it’s hard.
You’ll see that everyone has a style and quirks.
Let’s look at a python file that does something useful
Let’s look at the Ants project.
There’s a lot of code in Ants_Gui.py - Let’s take a look and read it.
Can we use the specification to help us read the code and solve the problems?
Sometimes on exams, you’ll be given some code, but not all of it. You need to learn how to adapt your thinking to fit this prewritten code. Let’s take a look at Where’s Waldo from the Summer 2014 Midterm. (Page 5).
- We notice a few things right away, like the recursion. How can this fact help us?
Write a really hard to understand piece of code & ask someone in class to understand it.