How the programming magic happens

Here’s your thought for the day: Programmers don’t really know how they program. I stared in bemusement at that simple claim for a couple of seconds, went on to read the whole thing, and then realized the truth of it.

I don’t really know how I program. I couldn’t describe it in a series of steps like I might be able to describe how I drive a vehicle, or even solve a math problem (whole classes of which generally have certain steps that, when followed in order, yield the solution like peeling layers off an onion). Come to think of it, I recall initially being thrown off in my first computer science classes in middle school when the teachers weren’t really teaching us how to program. They showed us lots of example programs and how they worked, but the real sink or swim moment was when you were asked to program something you hadn’t yet programmed before. No wonder so many students in my computer science classes were put off by it; they were being asked by their teachers to come up with solutions completely on their own, to a degree they had never yet experienced before in school.

Here is my general “process” when I’m programming something tricky:

  1. I think about the problem for awhile. If there’s a written description, I read it over until I grasp the whole of it in my mind, then I just kind of stare blankly at the screen. Sometimes I close my eyes. This can go on for a couple of minutes, so when I’m doing something at work, I’d rather not have the boss come in during these moments lest he think I’m not being productive.
  2. I start outlining how my solution will work. Sometimes I do this in comments directly in the IDE and flesh it out with code later; other times I just write it in Notepad, or even on a physical sheet of paper when I need to sketch something. If I have a good grasp of the problem, I might just keep it all in my head.
  3. I go research anything I don’t yet know that I will need to complete my solution. Generally this involves Googling. After many years of doing exactly this, I’ve become remarkably efficient at researching programming problems online. Oftentimes I have an API or some documentation in mind when I start my search.
  4. Here’s where the magic happens: I write the code. I can’t really explain it in any more detail than that. Oftentimes my planning was woefully incomplete (or just plain wrong), so I revise it as I go along on at this step.
  5. Testing, and then debugging if something isn’t working.

Kind of enigmatic, huh? I think I could do a decent job of explaining how the planning stage works (I identify the problem, then rack my memory for any prior experience I have with that sort of problem, combining various common programming patterns as necessary, until I have something that should yield the answer), but I can’t explain how the programming stage works. Even more problematic, sometimes I completely draw a blank when I’m planning out a solution, so I just go directly to the coding, writing out line after line until I’ve arrived at some code that vaguely does what I’m looking for. I couldn’t say how that works, and it’s not nearly as simple as “Here’s how to design the program, and then writing the code is a simple transformation process on the design.” No, it’s not even nearly that simple.

How would you teach someone to program? The only advice I can come up with is to use an iterative cycle of planning, research, coding, and testing until the problem is solved, and not necessarily in that order. Other than that, I would have to revert to teaching programming the same way it’s been done since time immemorial: by example. I guess I’d spend a lot of time picking good examples that cleanly illustrate important programming concepts. But at the end of the day that’s just showing what good programs look like and how they work, not how to actually program.

There’s a lot more artistry in programming than we computer nerds get credit for. It’d be like trying to teach someone to be a great painter. Yes, you can teach individual brush strokes and show students great paintings, but at the end of the day, that aesthetic and creative insight that separates a Van Gogh from an Adolph Hitler cannot be taught.

12 Responses to “How the programming magic happens”

  1. William Says:

    I think the best quote from that whole article and comments is “You can’t teach tall.”
    That said, I can’t say I agree with everything he says. The revision at the end certainly helps his case, however. I wonder if there’s any profession for which a process exists for the entire thing. Maybe that can’t exist because anything that was such would fail to be recognized as a profession anymore? Or is so basic that it’s everyman. Dunno.

  2. Kelly Martin Says:

    Lots of professions have well-defined processes for what they do, and most anyone can learn the basics of those professions in a reasonable time. One which comes to mind is stonemasonry. Anyone can learn the basic processes of stonemasonry; the main barrier here is the physical strength required to huff stone around. The thing is, anyone with sufficient strength and ordinary intelligence can follow the easily-understood basic processes of the craft to build a perfectly serviceable wall or floor. But it takes a master mason to make the wall look good; there are aesthetics to the construction of walls and floors and patios, and those are not readily subject to being taught in short order; rather, they are learned, when they are learned at all, only after spending a lot of time building stonework and looking at other people’s stonework and talking to others about stonework, in order to build an internal model of what will look good and what won’t. Some of the simple rules (“avoid cross joints”, for example, which is both an aesthetic rule and a structural one) can be taught in a few moments, but those are just first-order approximations. I watch, from time to time, a DIY show about stonemasonry, and they talk frequently about concepts like ratio and balance, but they can’t really explain them. All they can do is show you what they consider good work (and occasionally examples of bad work right before they rip it out) and hope you get it.

    Of course, elegance in engineering is different from elegance in craft; in craft elegance has to do with whether the result evokes the desired emotional response in the user/viewer, while elegance in engineering has more to do with whether the product is especially effective at meeting its designed purpose. But even the ability to recognize elegance is something that takes time to learn, and the ability to create it elusive in the extreme.

  3. jeem Says:

    This is a clear example of how Kelly and I differ: she read this post and thought about stonemasonry; I read this post and thought about baguettes. (Basic baguette baking is a simple process that anyone with the right ingredients [flour, water, salt, yeast] and a decent oven can follow, but it takes years of practice and fine-tuning before one’s baguettes start transcending this ordinary world.)

    In either case, though, aspiring stonemasons and bakers serve time in a formal apprenticeship, learning on the job from an experienced master. Perhaps this is a better approach for programming than traditional pedagoguery.

  4. arensb Says:

    For some reason, this post reminded me of calculus: there’s a certain art to doing integrals (and consequently differential equations) that AFAIK isn’t and perhaps can’t be taught; it’s just something you have to practice, and learn which functions “feel” as though they have integrals of the form f(g(x)), or f×g(x), or whatever.

    It’s a knack that I never picked up. Yet in a similar way, I can pick a programming problem apart and see whether it “feels” like a recursive or iterative problem, or whatever.

    As for your general process, don’t forget Richard Feynman’s Problem-Solving Algorithm:
    1) Write down the problem
    2) Think real hard
    3) Write down the solution

  5. William Says:

    Why are comments closed on the newest post?

  6. Cyde Weys Says:

    No idea. I’ve enabled them.

  7. William Says:

    WordPress upgrade tweakiness?

  8. Cyde Weys Says:

    Nope, I tried upgrading WordPress after the comments got all messed up.

  9. T2A` Says:

    1) Write down the problem.
    2) …
    3) Profit!

    I could only ever figure out half of calculus — the derivative part. Going the other way never made much sense to me. I had to take a differential equations course for my CS degree and that was just terrible. I never learned a single thing in that class. It didn’t help that the professor was beyond incompetent at explaining things. I’m not sure anyone ever learned what a differential equation was that semester. The general confuddled look on everyone’s face during class time was amusing but in a sad way.

  10. William Says:

    What the ellipses?

  11. drinian Says:

    What the ellipses?

    It’s a meme that started with an old episode of South Park many years ago.

    The process for programming that you outlined sounds a lot like the process for writing, especially non-fiction or research writing. And the actual process of putting (virtual) pen to paper and writing is just as mysterious to me as the act of programming. People have been thinking about the process of writing English much longer than code, and I don’t know of any really satisfactory answers.

    Certainly, it is possible for people to improve their writing or English skills. But if good writing were a skill that could be completely taught, there would be no Myspace or USA Today.

  12. llywrch Says:

    I’m not surprised in the least at this comment. It’s a well-known fact that short story writers & novelists have no idea how they write a story. For example, Faulkner once admitted that the origins of his _The Sound and the Fury_ was the mental image of a little girl in a tree, & his book was an attempt to explain her story.

    The basics of writing are clear — keep writing on a regular basis, rewriting is an important stage in the process, make friends with your dictionary & thesaurus, never be afraid to throw stuff out — but the actual process of how the glimmer of an idea becomes a successful story is a mystery even to the author involved. Some authors will pontificate about this to no end; some simply shrug & get back to writing — or procrastinating over writing.