Good Software Developer Uses Math
By: Roman Suzi
Tags: Math, Software Development

Good Software Developer Uses Math

There are a lot of discussions whether software developers need math to do their work. And the popular view is they do not. Here I challenge that assumption.

Photo by Gayatri Malhotra on Unsplash

First of all, I would like to clarify which kind of mathematics helps every software developer to be really good and versatile. I am not talking about math-heavy software development here, so in my personal observation, encountering mathematical problems may be a rare event. 95% of time developers (seemingly) are concerned with something else, especially, when they are engaged with the higher level programming languages. In those 5% of occasions it’s still quite manageable. For example, from time to time we may need to do a topological sort or implement some graph algorithm. It’s enough for a team to have at least one person to do it right, but I am not talking about those cases here. Knowing some areas of applied math or algorithms from computer science courses is a big bonus for programmers, but does not prevent people without competences in those fields to become professional software developers. Zero-coding revolution is not the main topic here either, but attentive readers can come to certain conclusions. The main claim here is as follows:

Good software developers reasoning is very similar to that of mathematicians.

On the surface it is not a surprise, because the code we write is equivalent to… mathematical proof according to the Curry–Howard correspondence: Programmers write proofs. Here it’s enough to say every function in the code is an instance of a proof. The whole code of the function can be seen as an example of the function’s type. And having a proof is equivalent to showing at least one example of the type. We are talking about constructive mathematics here. Those interested can take a look into one of the proof assistance, such as Coq, to see what it is about. Well, I’ve oversimplified it in this paragraph. There are a lot of details and nuances.

Good software developers construct better proofs. In order to do so, they develop a special kind of thinking, which includes logical or as some call it algorithmic thinking, and exactly this kind of thinking is similar to what mathematicians do.

Using mathematical thinking does not mean one should know cosines or abstract algebra or some other specific topics or constructs. What is valuable here is the way to reason itself, which can be developed even in high school. It’s completely another matter which constructs teachers use to teach mathematics, but using numbers, addition, multiplication, functions, equations, geometrical objects, it is already possible to give some basic skills and understanding of how mathematical reason works.

High school math content as such can be enough for most tasks in programming. However, it’s the special mathematical thinking we are after here.

Both programming and math are about manipulating symbols. In both areas symbols fall into place in a very strict way, which means both developers and mathematicians should be very careful about what they write and how symbols they write are connected with the rest of the system they are modeling. Both programming and math usually understand symbols as having some type, which strictly defines how that symbol can be manipulated.

One more hint of similarity for both kinds of reasoning is that both professions consider finding and applying patterns as a saver for time and cognitive efforts. Even the term refactoring, widely used in programming, has been probably borrowed from math, where it means removing a common factor out of the brackets:

ax + bx + cx + dx = (a+b+c+d)x

Clean code should read as prose. However, in my opinion writing clean code is quite different from writing prose. This text is not very good prose. It has syntax mistakes in it, typos, punctuation is not correct in some places. And even though I put some thinking into presenting arguments here in a certain order, natural language allows for much more freedom of expression, and my reader will hopefully understand what I am trying to say here.

Writing code in programming language or doing mathematical research gives much less freedom. Mathematical texts are not free from mistakes, but those are considered more like bugs in software. This is because both math and programming use formal languages to express knowledge.

Even though both math and programming keep creators inside certain limits, both fields provide unlimited possibilities to be creative. Sure, certain “functions” of mathematics are known from ancient times, many only for the past 200 years. Programming languages have more recently written libraries. The programming community is abuzz with Rust, so are mathematicians with homotopy type theory. In both fields it is easy to implement something by yourself. But in both fields that new construct can already be found in the vast libraries written so far.

Those in doubt, may still agree to the following:

Experienced programmers, who think they do not use math or do not need math, are just reinventing the math.

It is also important to note here, that while it is relatively hard to do both programming and math, it may be relatively easy to check proofs (if they are written in proof assistant language) or to run the program (written in programming language). It is even possible to verify (prove) some piece of code does exactly what it is supposed to do, though usually it’s quite costly and tests are written instead (tests can prove only that the code is wrong). This is because the logic of the written code is made easy, formalized for machines by humans.

Writing code or math on the other hand is not as simple as running some program. The catch here is that it is impossible to write an algorithm, which can produce an algorithm for every possible problem. Yes, we can move closer and closer to the ideal world by developing new tools and techniques. Yes, certain things, which programmers did yesterday may require programmers no more and accumulated libraries or artificial intelligence can replace them. Yes, AI can help work on mathematical proofs. Mindless programming jobs can disappear. Programming as a profession, however, will not in the near future.

For those who think math is nearly useless for programmers, please, think again. Best of you are doing it without realizing it. So learn some math to become a smarter developer, to align your system of reasoning with a much more advanced one with hundreds of years of “coding” practice. The good news is math is a very vast domain. Start with something elementary: set theory, abstract algebra, category theory, topology, mathematical logic, number theory. Find good introductions, which are not too much into applied mathematics, but stay with fundamental concepts. That is why I do not recommend starting with statistics or mathematical analysis. Try to practice with some “unusual” constructs: groups, graphs, categories, sets. You will discover math from a completely different angle than what you remember from high school. Start with simple constructs. If you love programming, you will like proving theorems. Of course, looking into graph theory may require knowing what is a set. But it’s like in programming using concatenation may require looking into what types the operation supports.

Practicing mathematics will give you an edge over time. You will start to see more patterns and discover that the code you write revolves around a couple of dozen fundamental patterns. You will also amplify your intuition for incomplete constructs (classes, functions, modules), which will help to prevent certain kinds of defects in the code. Maybe, to simplify, if you implement subtraction, your intuition will tell when to “invent” negative numbers to make things clean and beautiful again.

Actually, it works both ways. Programming reinforces understanding of math. Helps to think out of the box (of the “school math”).

Fast symbolic manipulation skills math education gives will make your code have less hard-coded things: x + y is slightly more complex than 2 + 3, but when we also need 2 + 4, 3 + 5, and 7 + 1, making generic function instead of three specific ones can definitely be less complex. Some words of warning are due here. There is one important difference between programming and math: The power of mathematics (and mathematical logic) is in abstracting from the problem domain. Meanings are not needed once we formulate the mathematical model, and appear again when calculations solves the problem. In programming it is usually good practice to call things by their names and attach meanings to constructs whenever possible. In this sense programming is more like physics, where connection with the real world is crucial.

However, math can still help here. Domain entities’ meaning is sometimes quite patchy. For example, it is generally understood that there are 60 seconds in one hour. It is so common sense, that the fact that a leap second can be added now and then rarely crosses the mind. (I have even seen cases where summertime transitions did not cross the minds of developers.) The lesson here is that busy programmers may rely on some assumptions, and only math can tell that the duration of a minute can be more than 60 seconds, just because we have a difference of two numbers. Ability to attach and detach meanings can help build more reliable software. And this is also what mathematicians do, though they spend much more time in the “detached meanings” state.

I’ve tried to sell mathematical (and logical) thinking to developers who consider themselves distanced (or even openly disliking) mathematics. In addition, and for everyone I can only add that mathematics has more concrete uses in computer science and theory of algorithms. For example, every developer should be aware how computational complexity of an algorithm can be calculated (at least estimated) in practice. Otherwise, their software will be open to unpleasant surprises. Basic knowledge of logic can make wonders to the conditions. Familiarity with graph theory can save many hours by applying well-known algorithms, and many more hours of debugging those algorithms. By this I want to say that mathematics can help it’s users directly, not only as a way to hone reasoning skills.