Deconstructing Ideas And Constructing Code • Using the Store-Repeat-Decide-Reuse Concept
Starting to code on a blank page • How do you convert your ideas into code?
One of the hardest aspects of programming is facing a blank page at the start of the process. You have a clear idea of the problem you'd like to solve. But how do you convert your "human" idea into computer code?
I call this the "Spanish problem". I'm lucky to come from a part of the world where it's common to speak several languages–human languages. However, my Spanish is stuck in that stage where I can read and understand it fairly well, but I freeze the moment I need to speak Spanish! ¡Mi español no es muy bueno!
A common comment I hear from many beginners and intermediate programmers is that they understand code quite well when it's explained to them or when they see it written down. However, they don't know where to start when they're trying to write code independently. This is the coding equivalent of my "Spanish problem".
There's no magic solution to this problem. However, I use the Store, Repeat, Decide, Reuse concept to help break down an idea and translate it from English (or whatever language you think in) into Python (or whatever programming language you code in).
Every aspect of a computer program falls under one of these four categories:
Let's see how we can use this concept to help with the "Spanish problem", and we'll look at some examples.
You can categorise what goes on within a computer program using one of these categories:
Store: You need to store some information within your program to use it later. Often, you do this by assigning data to a variable name. Data is key in any computer program. Later in this article, I'll claim that the Store part of this approach often includes some of the hardest decisions you need to make when planning a computer program
Repeat: You need to repeat a block of code several times in a row. The most common options for repeating code are using
Decide: You need the computer program to make a decision while the program is running. This is not a decision that you, the programmer, can take when writing the code. Instead, it's a decision you need the program to take during the execution of the code. You'll often use an
ifstatement for these decisions
Reuse: You have a block of code that you'll need to reuse in several places in your code, possibly using different data as input. Defining functions using the
defkeyword is one way of making code reusable
Reusing and repeating can seem similar, but there's an important distinction. You repeat a block of code several times in a row. You "package" code so you can reuse it in different parts of your program. You can also create functions even if you intend to use them once.
You should use these concepts as general guidelines to help you think about your program. Not everything falls neatly into just one category. Let's take a
while loop, for example. The
while loop falls under the Repeat part of this approach since it repeats a block of code. However, there's also a decision-making process that occurs in the
while statement: "Should the program repeat the code again?" The answer depends on whether the statement following
False (or more generally, truthy or falsy.)
We'll also discuss classes later in this article, which merge Store and Reuse into one entity.
We'll look at practical examples of using this approach soon. But first, let's talk about storing data a bit more.
There Are Many Different Types of Boxes to Store Things
I often ask students which of the four aspects they think will provide the biggest challenge when coding. Very few choose Store. However, that's the one I would choose as the trickiest of the lot.
The main reason I believe Store poses the biggest challenges is not just because you need to identify what needs to be stored—that too can be a challenge—but you need to decide which "type of box" to use to store these data. Which data type is best suited for the job?
There are times when the data type you need is obvious. An integer to store the number of people in a group. A string to store the name of a player in a game. But often, the structure of the data is more complex, and you need to consider several options. Should you store the students in a class as a list of strings, or perhaps a list of tuples, maybe named tuples? Or maybe, a sequence of dictionaries might work better? Why not a pandas dataframe along with other data relevant to each student?
The answer depends on many factors. It depends on what you want to achieve in your program, your coding style and preference, and other constraints and requirements. Indeed, there isn't just one "right" answer on how to store your data. There are always several options.
Thinking about these options before you start coding will save you from having to rewrite your code too many times as you change your mind about what data structures to use. Or worse still, you'll stick with the wrong choice because you've come too far to change!