State Management: Observations on The Similarities Between Coding and Music

Is it just me, or have other people noticed that software engineers are also often musicians, or at least, huge lovers of music? In my work and studies over the years, I’ve found music-making and coding use the same kind of thinking. In the following article, I’ll explain music from the perspective of computer programming using a common language, and hopefully, I’ll be able to illuminate similarities between these arguably disparate disciplines.

To start, I think the main way music and coding are similar is that at their heart, they’re both forms of state management. To explain this connection, let’s start with an example.

Let the Music Play

Begin by imagining the melody to your favorite song. If you can’t remember one, think about Fur Elise by Beethoven. Specifically, think about the opening trill between E and D# that opens the song’s first phrase. If you can’t remember Fur Elise, think about “Hot Cross Buns” or “Row Row Row Your Boat.” Hear the melody in your head and try to be aware of your feelings as you sing through it. Specifically, become aware of how your mood changes from start to finish; every note, the act of discerning it from the others, marks a distinct feeling inside. Do this a few times and you’ll become aware your mood changes as the song progresses. In fact, we could refer to the change in mood as a change in the state of our emotions, or simply, a “change in state.”

That word “state” should ring a bell to programmers — we work with state all the time in React components and Redux stores. In fact, the idea of a state permeates all of Computer Science, from concrete examples to more abstract ones. Physically speaking state can be a tangible thing, with the registers of the CPU and all electronic impulses moving through the computer at any given moment. PC Memory is also essentially a state with a large number of possible configurations. We can also think of state conceptually by defining it as what is inside our head as we conceive of an input, perform the logic with our brains, and end with an output. For example, run an algorithm through your head and you’ll know what I’m talking about. You’re tracking variables, and everything you’re tracking amounts to the state. Let’s connect this idea back to the experience of music-making.  

Now that we know what “state” is, we can understand how it can change within us as we make or listen to music. Is it really so far-fetched of an idea? I don’t think so, but whether or not you’re convinced thus far, notice I was able to describe music’s effect on us with the vocabulary of a well-known programming concept. This is good because it means we can use the same language to describe both coding and music.

Mood, or The Meaning of Movement

This leads to the idea that when music “moves us”, the mind is simply changing its state. People can acknowledge that music compels them to dance, and in this sense, the music is moving, and yes, compels us to get up and dance. Still, the music didn’t actually move a person with it’s proverbial arms. Additionally, is it not true that people will sit in stillness and still feel deeply moved by music? Since it is so, the feeling of being “moved” is an internal experience, a reaction to music, or a change in state. Physics is the basis for this claim, which links change to motion, as well as to time. We often say a program is “running,” which further links the concepts of change and motion, because it’s in the middle of changing its state.

Adding another layer to the discussion, it is also possible to say the state change is meaningful to the listener in some way. When I say “meaningful,” I mean we respond to music and express our reaction. Certainly, music is meaningful to people and that’s what makes it so wonderful. We might be able to say the same thing about computer programming in the sense that the programmer cares very much about the outcome of the program and this is a key insight: a change from one state to another matters a great deal to the consumer of that new state. Circling back, observe again I’ve again used technical jargon to describe the fundamental experience of music. Think about it: your mood is your state and your change in mood in response to the music is basically the same thing as logical operations upon a state object. The music flows, we respond to it, and then we make use of, or attribute, value to that experience.

You might be thinking, “But my mood wasn’t changing. I just listen to music and enjoy myself. I don’t track my mood during the piece. I really just have one mood during music listening: ecstasy!” This may be true but would you also agree you feel different after listening to enjoyable music? If so, you’re experiencing state change: moving from one state to another and then enjoying that experience and attaching value to it. Depending on their sensitivity, most people experience a state change in response to music.

Consider all of these points, play some music again, and reflect on whether you think what I’m describing is happening. If you’re still unsure, take this example: If I listen to some music to wake me up in the morning, I move from feeling tired to alert, and may even repeatedly use this strategy to wake myself up. Basically, I have access to a computer program for my body: my mood changes state, I find that new state useful, and therefore I attribute value to it. As such, the music automates a task I need to complete often, and this is exactly what technology is meant to do. If you agree with me, then congratulations, you’re thinking about music and programming as fundamentally similar concepts.

The Importance of Execution

Music and coding are related in other ways too.

For example, they both must be executed for them to have their desired effect, as opposed to being stable in space, like a painting. They’re both stored and repeated for later use, and both are inseparably tied to the technology available to them at the time. Just imagine what Frederic Chopin or Mozart would have composed with a computer from our modern era!

From another perspective, we might consider how the configuration of piano keys being pressed at any one point on a piano is astonishingly akin to the state of the memory registers in a CPU at any given point in time. It’s a neat idea! This and other little musings go to show that in thinking about both music and coding, it’s possible to see how people arrive at the idea of music and math having a lot in common.

Music and Coding: What are They Telling Us?

Another way in which music and computer programming are similar is that they are both representational. This is an interesting concept that deserves more explanation.  

What I mean by representational in computers is that you get to express a lot more than a simple command. When we write code, we are commanding the computer to perform millions of computations, most of which are beyond our awareness or comprehension. For example, if I render an image on an HTML Canvas, those ten to twelve characters of code equate to a very large number of computations by central processing units. Yet we are not completely aware of those computations. We know they happen but we don’t literally run all of those processes through our heads when we code. Rather, we acknowledge our code represents those instructions faithfully and perceive our code as representing something larger, the millions of computations the CPU or GPU must execute.

And music is nearly identical in this way because it represents something far greater than its intrinsic substance. Given a melody we like, we’d say its meaning is attributable to the feelings it evokes inside of us. It’s not the music that is by itself meaningful to us but all the feelings the music brings up that causes us to feel. In this sense, each note actually represents a complex series of executions, called life, something infinitely complicated we acquiesce to represent as music, and feel that music represents it accurately. For me, personally, this is probably the defining characteristic of music, differentiating it from just a collection of organized sounds.

One might be able to make an argument for the same similarity between programming and other forms of communication or other phenomena of nature, such as language.

Why Music and Coding are Special To Me

You may be wondering why I came to notice the similarities between music making and computer programming.

To start, coding found me largely by accident but it gave me a feeling that definitely wasn’t. In fact, it was quite special. It felt exactly like practicing the piano, something I absolutely love to do but with a few added perks…such as the feeling of euphoria after solving an algorithm. One rarely experiences this as a classical pianist, where the angst of playing a piece perfectly stems any lasting feeling of elation. With coding, there is great excitement; it bubbles up from deep down inside and takes over. It’s a discovery of movement, of power to control information. It felt quite good. Yet, when I looked up the solution from an expert, I realized how much more elegantly I could have coded my own solution, just like I could’ve performed Beethoven’s Fur Elise far more beautifully no matter how much I practice. Alas, I came full circle, where coding ended up being exactly like practicing an instrument!

So I think this deep connection is what connects the two disciplines. As I continue to study computers, I feel a deep similarity between playing music and coding that wasn’t immediately apparent when my coding journey began. I look forward to getting better at both in the next few years.

Apply to App Academy: http://bit.ly/AppAcademyOnline

Written by Adrian Jewell

Adrian Jewell is a software engineer, pianist, and instructor at App Academy.

Leave a Reply