Interact with your code
A great way to become a better programmer is to read code. As Andrew Binstock began his most recent Integration Watch column,
The programming section of Reddit occasionally receives posts from comp-sci majors and early entrants into professional programming who want to know about best practices for becoming a great developer. One of the consistent answers from experienced commentators is to read a lot of code.
Andrew shares several anecdotes about how he learned new techniques by reading through other codebases, but his essay reminds me of an experience I had while teaching an undergrad programming course in the early 1980s. You’ve possibly had a similar experience.
A student was implementing a complex numerical algorithm in FORTRAN – I forget what it was – and it just wasn’t working. The program compiled and ran through the data set, but the results weren’t correct. We looked at the source-code printout together, and neither of us could spot the flaw. It was late, and we were tired and frustrated.
Then I had an idea. “Let’s flowchart your code. Not what the algorithm is supposed to be, but what you’ve actually written.”
We didn’t have a whiteboard, so we grabbed some greenbar paper, laid it out on a table, and started freehand drawing boxes, diamonds and arrows. We walked line by line down the recalcitrant routine. The task was straightforward, but took some time. It was a complicated algorithm.
As we neared the end, the light went on. “That arrow’s not supposed to go here. It’s supposed to go there,” the student said. Eureka! Needless to say, the fixes to the code were quite simple, and the next iteration of the program returned the desired results.
Okay, this story isn’t about learning new techniques by reading other peoples’ code. Rather, I’m reminiscing about debugging through a code review. However, the point is that mere reading isn’t always sufficient to provide understanding. Sometimes, you have to interact with the code to truly comprehend what it means.
I’m no programmer, so pardon me if this is a dumb suggestion. But what if someone created a code analyzer that automatically flowcharted existing code? Is that possible? Has it already been done? If so, it seems to me that it would be a great debug tool for discovering logic errors in code.
– Paul
Instead of flowcharting I prefer to trace the code. Making it full of console or file prints telling what it is doing in a way understandable by humans, so you can check if the code is doing what you expect. Another option is to use a debugger.