How Do You Learn to Code?: the full story
Part One · A game in her head
Mila had a whole game in her head. A hedgehog would run along the bottom of the screen, apples would drop from a tree, and every apple the hedgehog caught would earn a point. The only problem was that Mila had no idea how to build it. So she knocked on the door of her big brother Ivo, who was sixteen and wrote code for fun. "How do you learn to code?" she asked. Ivo grinned. "You make something," he said. "Start with the hedgehog."
First Ivo explained what code is. Coding means writing instructions, called programs, that a computer can follow to do a job. A set of steps that solves a problem is called an algorithm, and even a recipe counts as one. So Mila wrote her game as a recipe on paper. Make an apple fall from the top. If it touches the hedgehog, add one point. If it hits the ground, start a new apple. It looked simple, which turned out to be the first surprise.
Part Two · Blocks
Ivo told her to begin with Scratch, a free coding tool released by the MIT Media Lab in 2007, made by a team led by Mitchel Resnick. In Scratch, you do not type commands. You drag colored blocks and snap them together, so you never have to worry about a missing comma. Mila drew her own hedgehog with a round brown body and a tiny nose. Then she drew a red apple. They looked ready to play, even though neither of them could move yet.
Ivo showed her a trick. Scratch is named after the way DJs scratch records, mixing sounds together to make something new. In the same spirit, Scratch lets you remix: you open someone else's project, look inside, and change it to make it your own. Kids have made more than a billion Scratch projects, so somebody had almost certainly made a falling thing before. Mila found a game with falling snowflakes, peeked at its blocks and saw how the falling worked.
Her first program needed two new ideas. One was a loop, a block that repeats the steps inside it, so the apple could keep falling without Mila writing "move down" a hundred times. The other was a conditional, an "if" block that checks whether something is true, such as whether the apple is touching the hedgehog. In Scratch, both of these live with the Control blocks. Mila clicked the green flag to start her game. The apple fell. It fell straight through the hedgehog.
Part Three · Bugs
That was her first bug. Finding and fixing a mistake is called debugging, and Ivo said that every coder he knew did it all the time. The first step in debugging is usually to make the problem happen again, so you can watch it. Mila clicked the green flag five more times. The apple passed through the hedgehog every single time, and her score stayed at zero. At least the bug was reliable. Now she only had to figure out where it lived.
Ivo handed her a rubber duck from his desk. "Explain your code to it," he said, "one block at a time." Mila thought this was a joke, but it is a real method, named after a story in a book for programmers. People often spot the answer while explaining a problem out loud, even to someone who knows nothing about code. Halfway through her explanation, Mila stopped. Her "if" block was outside the loop, so it checked for touching only once, before the apple even fell.
Mila wanted to know why mistakes are called bugs. In 1947, workers testing a computer at Harvard University found a real moth trapped inside it. They taped the moth into their log book with a note saying this was the first actual case of a bug being found. It was almost certainly a joke. Engineers had been calling little faults "bugs" since at least the 1870s, and the inventor Thomas Edison used the word in a letter in 1878. The moth was simply a very literal bug.
Part Four · Typing for real
The history of debugging goes back even further. In 1843, a mathematician named Ada Lovelace published notes about a calculating machine designed by Charles Babbage. Her notes were three times longer than the article they explained, and they included step-by-step instructions for the machine, which is why she is often called the first programmer. Babbage himself wrote that she found a serious mistake in his math. Biographers still argue about how much of the work was hers, but the first programmer was also a debugger.
Once her game worked, Ivo showed her the kind of code he wrote, in a language called Python. Instead of blocks, he typed words. Python was started by Guido van Rossum during his Christmas holidays in 1989, and he named it after a British comedy show, Monty Python's Flying Circus. Python uses spaces at the start of each line to show which lines belong together. And the ideas Mila learned in Scratch, such as loops and "if" checks, carry straight over.
"But how does the computer read Python?" Mila asked, staring at Ivo's screen full of words. It does not, exactly. A computer's processor understands only its own machine code, built from binary digits. Code written for people, which is called source code, has to be translated by another program first. Grace Hopper, an early computer scientist, pushed for languages built from English words. She was told that computers did not understand English. She kept working anyway. By 1952 she had one of the first translators running, and in 1959 she helped create COBOL, a language built from English words.
That weekend, Mila's little cousin played the hedgehog game for an hour and found two new bugs. Mila did not mind. She knew where the duck was. So how do you learn to code? You pick something you want to make. You build it in small steps, break it, and fix it. Then you try something harder. Try it without a computer: write the steps for making a sandwich, and ask someone to follow them exactly as written. Count the bugs.