Think before you code

Victor Hamzat
5 min readFeb 18, 2023

--

Often we get so excited about what we want to build that we skip the most important part — good thinking and planning. This might be fun at first, especially when the caffeine kicks in, but you’ll soon find out that you’ve wasted a lot of time, which could have been saved with proper planning. “The best line of code is the one you don’t have to write” is one of my all-time favorite coding proverbs. It could be taken to suggest that careful planning can help you save time. To effectively plan a coding project, you need to:

  1. Know the issue you’re trying to solve,
  2. and research potential solutions

Know your problem

Charles Kettering once said, “A problem well stated is half solved.” Understanding your problem will enable you to avoid taking actions that might, at the very least, move you further away from the answer. Without a good problem statement, you’ll be trying to find a bug without any error message, which is possible but stressful.

Proper Research

This is a significant step in getting the problem solved. It mostly comes down to asking the right questions. You’ll have to check for available solutions and what modules, libraries, services, etc. can help you solve the problem. Typically, I keep track of all my research findings in a markdown file called research.md . This would be important for other people reading your code and adding attributions when necessary.

Many thinking paradigms can help you solve problems efficiently. But in this blog, we’ll discuss a mathematical thinking paradigm.

What is mathematical thinking?

Mathematical thinking is a way of thinking that involves using mathematics to solve real-world problems. Mathematical thinking is crucial in various sectors like science, technology, economics, and even our daily lives, as this book by Brian Christian and Thomas L. Griffiths highlights. As programmers, we constantly try to solve problems and ensure they are well-optimized, but this would not be easy without a solid foundation in math.

Let’s say we are building a password generator. Without proper thinking and planning, we can come up with this.

The code starts with creating the variables that store the possible characters we want in our password. The characters are then concatenated and randomly sampled to match the respective password length.

This is meant to be a secure password generator. At first sight, it looks just right, but looking into it, you’ll find a few lapses. First, the code just samples the possible characters, but the values returned aren’t going to be unique. Here’s a sample value: iaE6hTGtaOHV This makes the password easier to crack. Secondly, the variability of the characters isn’t certain. Let me explain what I mean. With this approach, you can have a password just like this: abcdefghijkl . The chances are low¹ but it is possible.

Password Generator: With mathematical thinking

Now let’s think of the problem we are solving. A password generator is meant to generate secure passwords, so how can we ascertain this?

  1. A way of doing this is through the password length. Four-digit passwords are easier² to crack compared to eight, twelve, and so on. We can choose 12 as the default number of characters for the passwords.
  2. Another way of ensuring password security is through the variability³ of the password characters. A password of all lowercase would be far easier to hack compared to a password that is a mixture of lowercase, uppercase, and digit characters.
  3. Also, we should avoid having passwords with repeated characters.

Taking all this into account we have this code.

The code builds on the previous example with some major changes. I added the individual counts for each character group (i.e., lowercase, uppercase, digits, etc.) and asserted that they added up to the password length. I then randomly sampled each character group and joined them into a single string. With this new approach, you can notice I switched from random.choices to random.samples . I did this because the latter ensures the values are uniquely sampled. I then finally uniquely resampled the crude password to change the character placements.

Through mathematical thinking, we were able to break a problem into distinct sections and account for them in our solution based on mathematical proofs. This example is just to show how you could tackle any problem pragmatically; it saves time and helps you develop near-perfect solutions.

Tips on improving your mathematical thinking

Mathematical thinking can be the difference between good and terrible code, and it is also a really good skill to have in your tool belt as a programmer. It may appear perplexing at first, but that is how most people experience things. I’ll give you a few tips that I think can help you in your journey of learning more about mathematical thinking.

  1. Practice: This can’t be overemphasized. You need constant practice to get better at thinking mathematically. It is like learning a language; just a few days without learning it can be disastrous. You have to solve lots of problems and work at them, just like with any other skill.
  2. Take a course on discrete mathematics: Discrete mathematics can help you hone your problem-solving skills, which are necessary for programming. Programmers who are successful in discrete mathematics will be able to generalize from a single instance of a problem to an entire class of problems and to identify and abstract patterns from data.
  3. Learn by example: sometimes the problems we face while programming has been faced by someone else. You can learn from how they approached the problem and improve on it if need be.

Thanks for reading

Footnote

low¹: the chance of having abcdefghijkl password from 74 password characters

calculation

easier²: a 4-character password with 74 possible password characters and no repetition would have

While a 12-character password with the same conditions have

variability³: a 12-characters password with only lowercase characters(26) and no repetition would have

While a 12-characters password with variability(ie. a mixture of lowercase, uppercase, and digit characters(62)) have

--

--