Programming: Math or Writing?
Tags: programming, math, writing
Published: 2019-04-02 07:51:07

---
title: Programming: Math or Writing?
published: true
tags: Programming,math,programming,writing
canonical_url: https://henrikwarne.com/2019/03/30/programming-math-or-writing/
---

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.

## Like Math

[![](https://henrikwarne1.files.wordpress.com/2019/03/formulas.jpg?w=531&h=299)](https://henrikwarne1.files.wordpress.com/2019/03/formulas.jpg)

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”](http://devlinsangle.blogspot.com/2015/04/the-importance-of-mathematics-courses.html).

**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.

## Like Writing

[![](https://henrikwarne1.files.wordpress.com/2019/03/story.jpg?w=508&h=286)](https://henrikwarne1.files.wordpress.com/2019/03/story.jpg)

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](https://dev.to/henrikwarne/exercises-in-programming-style-37ag)), 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.

## Conclusion

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?