What Programmers Can Learn From Mathematicians
A second rambling post in a confusing and incoherent series
A couple years ago, I blogged about the intersection of math and programming. It was inspired by This StackExchange post and was supposed to be passionate and informative, but when I reread it now it comes across as rambling. This is unfortunate, considering that to write it I conducted four “interviews” (they were random out-of-the-blue Facebook texts) with past college friends, including a math major who took programming classes for fun.
So I will tag this as “rambling” and pick up where I left off. Whether I get anywhere is anyone’s guess.
First Perspective: “What Does Mathematics Have To Do With Programming?”
A mathematician on StackExchange (linked above) said the following:
First off: I am a mathematician — a professional one (in that I get paid for doing maths). I am not a programmer. I do do some programming, but very definitely of the Cargo Cult variety (see first comment to https://tex.stackexchange.com/q/451/86 and my response) and nothing of the sort that would normally bring me to this site (indeed, I registered here to post this answer after seeing a link to it in the TeX chat room).
The summary of my answer is: Mathematics is Programming.
….
The other point I wanted to expand on a bit was why as a programmer you should not be scared of learning a bit more mathematics. It’s not the Deep Connections, nor the usefulness. It’s that your ability to program a computer can directly help you learn mathematics. I just want to mention a few.
Understanding variables. So many people get confused by simple statements like “Let n be a natural number …”. Or “Let epsilon > 0”. There are places in mathematics where it’s important to remember the scope of a variable. These are all commonplace in programming. Learn to translate a mathematical statement into a program and you’ll find it much easier to keep track of what’s what.
The nature of proof. If you’ve ever written a test, or written a program to be used by someone else, then you understand the core of proofs. When you do that, you have to know that whatever the user puts in, you can deal with it (insert obligatory xkcd reference here). That’s all a proof is! A demonstration that whatever the “user/universe” puts in, the statement will hold. Experimentalists will lean to the “If it works under normal circumstances, it’s true” but programmers know that there is always that kid who will try Alt+G+Shift+ÅØÆ just to see what happens.
If you choose not to read the above (or the aforementioned StackExchange link), it essentially makes two arguments: That math is programming, and that math is useful to programmers. I especially want to emphasize the last paragraph above: A proof is a demonstration that whatever the user puts in, the statement will always hold. A scientist will learn to “normal circumstances,” but a programmer need only hit the run button.
Second Perspective: Roman Suzi
Someone responded to my blog post with something that was probably better than the post it was responding to. It said:
For me math is more a way of creative thinking than specific areas like trigonometric functions you are using as an example.
I have a new insight after reading your article. Math is like a squared paper for programming. It helps the system programmers build to perfectly align, even when parts are on the opposite side of the “paper”. It gives a grid, tidy lines to design your code.
It may be not that obvious in the simple questions like interview questions, but it is of real help in bigger projects to be able to grasp the system complexity by having in mind mathematical constructs, which are used.
For example, Haskell leverages Category theory heavily, making different parts of it align like magic.
Roman included his own article link, where he emphasized that what was valuable in math was not just specific subjects themselves, like arithmetic, algebra, and trigonometry, but the underlying way of thinking.
Third Perspective: Rich Dlin
In a blog post, a math tutor wrote:
Math is not about getting the right answer. Sounds silly right? It isn’t. Here is the official statement:
Mathematics is not about getting the correct answer. But math does give us the ability and insights to prove that “the answer” is correct.
Mathematics is a way of thinking. It is a philosophy, an art, a language and a science (maybe even in that order). Math is not “how do I do this”, but “why does it work when I do this?” It’s about using a foundational set of axioms (which are assumptions taken as true) and then using logical deduction and induction to study the blossoming conclusions.
— Source
That was the impetus for writing this now. I was thinking about how programming can become an obsession with getting something that “just works.” Is the feature complete? Submit it. Do the test cases pass? Submit it.
This kind of results-oriented thinking is probably part of the appeal. For some, programming may even seem like a perfectly fair meritocracy: Without needing exorbitantly expensive science equipment, or ultra-fast Internet, or initial funding, a person can write code and see almost immediately whether it works or does not work.
But if all a programmer focuses on is finishing, then he/she is prone to do things like:
- Blindly copy and paste things from StackOverflow
- Ship temporary hacks and fixes (though the link is a YouTube video about Half Life source code, so it is a bad example. The code did what it was intended to do until Black Mesa came out)
- Find solutions to problems, but without taking the time to understand why the solutions work. I am pulling this out of thin air, but I imagine that a programmer who understands why solutions work and past attempts fail will become orders of magnitude better than a programmer who simply completes features and never tries to learn from past mistakes
This is analogous to how math is taught, and how it can either be treated as a generalizable way of thinking, something that helps us in all problem-solving, or a frustrating high school requirement where students are subjected to performing pointless calculations until they perform well enough to get some arbitrary percentage of calculations correct.
This is reminiscent of a Reddit comment on how LeetCode can be beneficial, iff the programmer tries to understand the underlying concepts behind the LeetCode questions. I have half a mind to spin this off into another blog post, I just with the Redditor had a more professional username.
Motivation
Is programming math? Is computer science really science? I posed this question to Smack, who said a lot of these things boil down to the question of “is a hogdog a sandwich?”
But that’s not really the motivation. Computer programming arguably came into existence in 1843, with Ada Lovelace’s code — computer science itself is arguably much younger or much older than that, depending on whether you define it by its mathematical origins, or by when it was formally taught in a university as its own discipline.
Programmers are situated in a (sort of) new, rapidly developing field, and so I find it productive to use other disciplines as points of reference. Do programmers use the scientific method? Not always, but they can learn something from it, and programmers in turn can use their own skill set to contribute to scientific progress.
In a few hundred years, after GPT-9 inadvertently reaches out to aliens and humanity is wiped out by Trisolarans, historians will be free to analyze programmers and the impact they had on history. Maybe they will be treated as their own chapter, or maybe they will be connected to the scientists and mathematicians who came before and after them.
Relationships are useful, at least my in opinion. Relationships make things…er…relatable.
Closing Thoughts
Above is that same stupid crayon diagram from the last post. I received an interview take-home question many years ago, and I did not pass:
You have a drone located somewhere on an XY coordinate plane, and you receive a series of XY coordinates for ships you would like to take pictures of. You need to take pictures of all ships. You receive a field of vision for your camera, and you need to return a list of camera yaw angles for the photos you will take.
Does anyone know the answer? Greedy Algorithm? Please let me know in the comments.