Anyone who has tried to look at somebody else’s computer code — especially in the likely event that it hasn’t been well-commented — knows how hard it is to figure out what’s going on. (With sometimes dramatic consequences.) There are probably numerous reasons why, having to do with the difference between heuristic human reasoning and the starkly literal nature of computer instructions. Here’s a short paper that highlights one reason in particular: people tend to misunderstand code when it seems like it should be doing one thing, while it’s actually doing something else. (Via Simon DeDeo.)
What Makes Code Hard to Understand?
Michael Hansen, Robert L. Goldstone, Andrew Lumsdaine
What factors impact the comprehensibility of code? Previous research suggests that expectation-congruent programs should take less time to understand and be less prone to errors. We present an experiment in which participants with programming experience predict the exact output of ten small Python programs. We use subtle differences between program versions to demonstrate that seemingly insignificant notational changes can have profound effects on correctness and response times. Our results show that experience increases performance in most cases, but may hurt performance significantly when underlying assumptions about related code statements are violated.
As someone who is jumping back into programming myself after a lengthy hiatus, this stuff is very interesting. I wonder how far we are away from natural-language programming, where we can just tell the computer what we want in English and it will reliably do it. (Guess: pretty far, but not that far.)