On the topic of the "theory" (mental model) of a program, I recommend John Ousterhout's book "A Philosophy of Software Design". You get such gems as:
"... the greatest limitation in writing software is our ability to understand the systems we are creating."
"Complexity manifests itself in three general ways... change amplification, cognitive load, and unknown unknowns."
"Complexity is caused by two things: dependencies and obscurity."
"Obscurity occurs when important information is not obvious."
"The goal of modular design is to minimize the dependencies between modules."