Programming 101: Key Mental Models
Programming is about casting spells on machines so that they serve your will.
Okay, okay. So maybe it’s not really magic. But it is magic-like. You have to learn a cryptic language, and you have to speak (type) the symbols in the right order for your spell to work properly.
So why does programming have a reputation for being hard? Because there are many pieces involved, which require many different “Mental Models”. What’s a mental model, you ask? A mental model is something in your head that helps you make predictions about how things in the world (people, animals, machines, etc.) will react in response to your actions or events in the world.
Suppose you’re a teen and you stay out late—past midnight on a school day. You know your parents will be very upset with you, and possibly yell at you the next day. How do you know that? You have a mental model of your parents! Another example: Don’t stare a dog in the eye because you might make it mad and it will bite you. How do you know that? You have a mental model of dogs.
A mental model doesn’t have to be 100% accurate. It’s merely a guide for your expectations and actions. You create mental models constantly and you constantly update your mental models in response to new information.
So let’s go through the mental models you need to know for programming.
Mental Model: Programming as Communication
Setting aside colorful magic, wizard, and spell metaphors, how should you think about programming?
Programming is communicating directions to a computer. So, you know how you sometimes give driving directions to other people? It’s like that. But instead of driving directions to a person (in English), it’s directions to a computer (in a language like C# or Java) for taking information and either
- Storing the information in memory, disk/USB drives, across the network;
- Calculating new information by using information in equations;
- Displaying the information.
And technically it’s only storing and calculating, because displaying information is a kind of storing of the information on screen or on paper.
So if someone asks you what programming is, tell them:
B. Computers only know how to follow three directions:
- store information,
- calculate new information, or
- display information.
Mental Model: Programming as Communicating With Computers
But how exactly do you communicate with a computer? Can you simply talk to it? Technically yes, but let’s pretend our computer doesn’t have voice recognition like our smart phones. And let’s continue using “communicating with people” as our starting point for understanding programming computers.
When we communicate with other people we simply talk and if they’re paying attention, they hear us and respond appropriately.
When we’re communicating with a computer, instead of speaking words in English, we’re:
- Typing code from a computer language into an editor.
- Saving our code as a file on the computer. And then telling the computer to
- Run the code.
Mental Model: Coding as Storytelling
Almost every well-written story consists of three parts: a beginning, a middle, and an end. In the beginning, the hero encounters a problem. The middle is spent searching for a solution. And in the end the hero solves the problem.
Similarly, almost all code also has a three-part structure:
- Input: getting information from the user, or from storage or from the network
- Processing: running calculations on the information
- Output: displaying the information to the user, or storing it, which may involve sending the information across the network.
Mental Model: Syntax as Grammar
Just like you can’t randomly mix words in a sentence and expect people to understand what you’re saying, you can’t randomly mix programming terms and expect the computer to run your program without crashing.
English has a grammar, and Programming Languages have a syntax. When you hear the term “syntax” think grammar. Bad syntax leads to your code crashing, just like bad grammar leads to people misunderstanding you (a kind of human crash).
Mental Model: How Software and Hardware Interact
A computer consists of storage (hard drive, usb drive), memory (RAM), a processor (Intel or AMD), and a graphics card (NVidia, AMD). Yes, there’s the network too, but let’s ignore it for now.
- Your program is kept in storage.
- When you run your program, it gets moved into memory.
- The processor reads and executes each line of code in memory.
- Any code for displaying information gets sent to the graphics card.