February 9, 2023 ~ 4 min read

How to think like a programmer

When you’re starting to learn to program, a lot of things can seem overwhelming. One of the questions that come up is: “How do I think like a programmer”

I see all this code, and I can understand what it does, but when I try to come up with it myself, I don’t know where to begin.

1. Break problems into smaller problems until they are easy to solve

When you think about something you want to build, it can often seem overwhelming because you don’t really know where to start.

Let’s say you want to build a minesweeper clone.

Here are the requirements:

  • You have a 2D grid of squares where each square is a hidden value until you click on one of the squares.
  • There is also the random generation of the location of the mines part and there is the calculation of how many mines are adjacent to one square so you can show the player the necessary information to solve the game.
  • Player needs to be able to ‘mark’ mines.
  • The game should be lost when clicking a mine.

This is pretty overwhelming to start, where to begin?

You begin at the smallest possible problem

(Let’s assume we’re writing in Python)

What is the smallest problem I can solve?

> Answer: I need some way to display the grid the player. So I need some kind of thing that draws. So I will look for a library that does this. For example https://wiki.python.org/moin/TkInter

What is the smallest problem I can solve?

> Answer: Well I can now draw stuff, maybe draw a square? The board is a square, so I will draw a square. This means figuring out how the library works, following a tutorial, etc until you can draw a square that is the size of the grid/board.

What is the smallest problem I can solve?

> Answer: Ok now I have a square, but it doesn’t have any cells in it yet. Hmm a cell is basically a small square. Let me draw one smaller square on the grid.

What is the smallest problem I can solve?

> Answer: Well I have a cell in my grid now, but it’s just one cell, I need multiple cells, evenly spread out over the grid. Let’s start with a row of cells. So you figure out the math to print these squares evenly spaced. You probably use a for loop for this to make your life easier.

You get the idea

As you get better at doing this, you will figure out shortcuts. You will make connections that tell you, ah I’ve seen this type of problem before, I KNOW I will need this particular thing in my program, so I will start with that.

For example, how I would start since I’ve done this sort of stuff before:

I start with the data represenation of the 2D grid, probably in a 2 dimensional array. Probably as an instance variable of a class so I can have some useful methods on the class like: reset_board(), generate_mines(), …

This approach will get me a cleaner and faster result. But for beginners it’s not easy or obvious.

2. Constantly refactor your code to make your code more readable

Once you have some code that works, at first it will probably be not that nice to read. There will be a lot of duplication, bad abstractions, … It’s really important that you clean up and rewrite pieces of logic until it’s clean. This will improve your ability to write clean code from the start. Always maximize readability. Don’t try to optimize for speed.

http://wiki.c2.com/?PrematureOptimization

Make it work.
Make it right.
Make it fast.

(In that order and priority)

Summary

  1. Divide big problems into smaller problems until they are easy to solve.
  2. Work on a smaller problem until that problem is solved.
  3. Refactor the code until it’s the best version of the code you can think of. Improving readability should be your goal.
  4. Go to step 1 until you’re done.

Addendum

There are many other pieces to the puzzle of getting good at programming. A noteworthy mention is that you should focus on how to structure data.

This is a more advanced concept to learn when you’re starting out.

Code emerges from your data structures. Data is hard to change, code is easy to change.

Data defines what code will look like, not the opposite way around.