Program in your head

I was thinking about something. Programming is mostly done in the head. Eventually your fingers type something on the keyboard. But the main part — the important part — is done in your head. Sometimes I just catch myself writing things. And they make sense afterwards. Just today a colleague congratulated me on a small refactoring I proposed and implemented. He said he couldn’t have done the same thing (in the given amount of time). And I didn’t really think too much about any of the steps. I just knew the kind of place I wanted to end up at. Then I took steps into the direction and the result presented itself in the end.

That wasn’t hard. That was the result of deliberate practice and a lot of experience.

Hard is to do it all in your head before you sit down to write code. It’s kind of like taking the Rubik’s cube and spin it in your head in all the right ways until you solved it in your head. Then you can get to it and take in into your hands and solve it. But I almost guarantee you: You won’t actually do it. Because the challenging, interesting part was already done. Inside your head.

And that’s what I want to do with code.

A few days ago I read about the EVE programming environment. It’s interesting and warrants a deeper look and probably a story just by itself. While reading about it something caught my attention. I don’t quite recall what the exact words were, but it was along the lines of: „[…] use it to program an algorithm, like tic-tac-toe […]“. In the beginning I just read over it. But somehow it did not let me go.

I haven’t written a tic-tac-toe algorithm in my life as a developer or while studying. But I have since reading that sentence. In my mind.

I started in a way that seems to me like the way an uninitiated would start. I imagined what it would look like. I am most familiar with Ruby, so I imagined it as a command-line application/script. My output/GUI would be text.

But I realized that this wasn’t important. I even changed my visual representation later on. Mind you, not really. I just changed the way I imaged it would look like.

The rules of the game are pretty simple, and that’s one of the reasons it lends itself almost too perfectly for this mind exercise:

  • You have a 3 x 3 matrix of fields.
  • There are two players.
  • You take turns to play.
  • One player places an ‚x‘
  • The other player places an ‚o‘
  • You win, when you can place three of your marks in a row.
  • Your row can be horizontally, vertically or diagonally aligned.
  • If no player manages to have a row, it’s a tie.

So what do you actually need to program?

  • The algorithm needs to be able to „scan“ the playing field.
  • The „computer“ needs to keep a representation of the playing field with its marks (x and o) in memory (or somewhere else?).
  • The computer needs to be able to decide where to put the next mark.
  • The computer should try to win.
  • The computer could know „the best“ or „advanced“ tactics to win.

If you fancy it, you could try to have the computer play all possible outcomes for a certain play/turn and rate them internally and choose the best, or something like that.

I bet your mind already made all kinds of assumptions. And you thought about that algorithm. It doesn’t matter what language you would write it in. That’s an exercise for your fingers and perhaps to practice or learn the syntax of a certain language. Perhaps, you don’t know how to program a computer or write a programming language. But you could still talk me through your algorithm. The tradeoffs you see and the decisions you made.

And now? I solved this in a 80/20 way in my head. I am sure there are edge cases that my design did not yet include. But generally I am satisfied with my result.

What about you? Did you find an algorithm while reading this? Perhaps in a few days? I’d be happy to hear about it. Write me an email or tell me on Twitter and let me know.

Next steps could be something like the following:

Take care.
Holger