Is programming like math, or is it like writing? I think there are elements of both in it, even though programming is a discipline of its own. Nevertheless, it is interesting to think about what aspects are like math, and what aspects are like writing. Maybe it will even lead to a better understanding of what programming is.
Sometimes, the domain of the program requires math, for example machine learning, graphics programming or financial modelling. However, here I am interested in parallels with math that apply regardless of the domain.
Problem solving. A program is an instruction on how to solve a problem. In order to write the program, you need to decompose the problem into smaller problems, just like when solving a math problem. You need to consider what input you need to solve the smaller problems, and how to combine those results in order to get the solution. You also need to consider all possible cases (“What if this information is missing?”). All this is very similar to how you work out the solution to a math problem.
Abstractions. To us programmers, our programs feel real. There are layers, sockets, object graphs, functions, hash maps etc. But in the end, the running system is just currents in electrical circuits. Everything else is abstractions that are useful when constructing and reasoning about the programs. The ability to reason precisely about abstractions is also developed in mathematics. Keith Devlin argues that this is the reason programmers benefit from learning math: “Why Universities Require Computer Science Students to Take Math”.
Functions and numbers. There are a many examples in programming that are either actual math, or very close. Examples include: functions (transform an input to an output), binary and hexadecimal numbers, Boolean logic (combining and, or and not), and big O notation for analyzing algorithm performance. Also, recursion is very much like proof by induction. And of course when we get down to machine instructions, it is all (binary) numbers and various forms of calculations.
Programming is similar to writing (at least non-fiction, but maybe even fiction). Even the words we use suggest that: you write a program, using a programming language. Here are some specific ways they are similar:
Communication. Successful programs have a tendency to grow large and require many collaborating programmers. Therefore the task of the code is not only to communicate with the compiler (syntactically and algorithmically correct code), but also to communicate the structure and organization of the program to other programmers. Otherwise the program will become very difficult to modify and expand.
This structuring of the code is quite similar to good writing. When writing, you need to tell the story clearly. You need to have good overall structure that follows a logical order. All parts need to fit together so the text is easy to understand. The layout is also important. A wall of text is harder to read than text divided into paragraphs. The same applies to programs. A 300-line method is harder to understand than the combination of several smaller well-named methods.
*Editing and revising. * For me, the process of writing a program and writing a text (for example this blog post) is remarkably similar. In both cases, I have an idea, and I spend some time planning what should be done. Then it is a very iterative process of coding/writing, with frequent refactoring/revising. It is very hard for me to get it right on the first try. It is much easier to gradually adjust and refine. Once I have something written, even when it is far from perfect, it is relatively easy to revise and improve it. This applies equally to code and text.
Style. There is quite a bit of room for individual style in programming, just as there is in writing. Imagine 100 people writing a one-page instruction on, say, how to make spaghetti carbonara. Even if they all agree exactly on how to make it (which I doubt), I don’t think any of the pages will be exactly the same. There are just so many ways to express the same basic steps. And I think the same is true for programs. Even for a problem as simple as the term frequency problem (used in Exercises in Programming Style), I don’t think any of the solutions would be exactly the same if 100 programmers solved it. The basics are the same, but there are so many ways you can vary the implementation. So just as in writing, there is room for individual style in programming.
Those are some ways programming is like math, and some ways programming is like writing. However, there are also many ways in which programming is its own thing. For example, programs are typically continually modified and expanded – they are never done. Also, they are ultimately run, and the runtime behavior adds another dimension, separate from the act of creating the program.
Still, it is fun to look for similarities with other areas. It is also interesting to ask whether a good writer is more likely to be a good programmer than a poor writer. Or whether being good at math helps you become good at programming. What is your opinion?