Parallels in Music and Code
When I first started learning React, I found it challenging. I found it difficult to focus and it was hard to grasp the new concepts at first. I also found myself listening to Poulenc while I studied. What was going on here?!
My background is in music composition. I have only recently changed my career to become a fullstack developer and my initial urge upon learning code was to get as far away from music as possible. I was changing careers for a reason after all! But I soon found there to be many parallels between code and music, specifically writing code and composing music. Although I have tried to avoid music, I keep finding that I come back to it, and it has more and more relevance with what I am creating with code.
Art?
Fundamentally writing music and writing code both involve a similar activity; they both involve making something. Music is definitely an art. I would argue that even though designing with code is certainly a craft, it can definitely be an art as well. Of course, philosophers have been debating where to draw this line for centuries, but they both boil down to creating. Music is written in the form of notes and rests on a page and rendered by live performers. Code is written in the form of its language and rendered as an app. This, of course, is only the beginning of their similarities.
Language
There are obviously many computer languages in existence, but fundamentally they are all similar. Over the years, new languages have been developed, expanded with libraries and used to create different types of programs. Music is also a language of sorts with different eras of music representing the “flavor” of the language. This is where I started thinking about how React compares to Poulenc. Consider the following:
Javascript is a huge language right now. It dominates the web, not just on the front end but it is sometimes used on the back end as well. We might even say, we are in the “Era of Javascript”. There are eras in music too. The Classical Era (c. 1750–1800) represents composers such as Mozart and Haydn as well as early Beethoven. Beethoven was a bit of a unique case, although Schubert is maybe similar. Beethoven took the fundamentals of classical music, the harmony, dynamics and articulation, and expanded the forms such as sonata form and rondo form into the beginnings of what would define Romantic Era style. At its core, it was still Classical, but the forms were now being expanded and modified. Consider how Javascript has various conventions as a language, but different libraries can be added such as React and Angular, expanding the form and layout of the language. As I discovered this added complexity, this is where I found myself listening to an impressionist composer from the Romantic Era. At their cores, Javascript and Classical-Era music are both fundamental to a time. And both have libraries that expand them into something else. All languages evolve over time; music and code are no exceptions. I can only imagine that writing code in C is probably like composing monophonic Gregorian chant by comparison.
Form
No, not HTML forms. Code has a format much like how music has form. Object-oriented code is organized into classes and methods. Music is organized into sections. There is often an “A” section and a “B” section. When music consists of two contrasting parts like this, we call it binary form (not to be confused with binary code!). This can of course be expanded. If parts of “A” exist at the end of “B” then it is rounded binary form. Often there can be repetition of the sections. If we further expand this and add a section that develops the themes of “A” and/or “B” then that is called sonata form. Imagine progression of methods calling methods over and over as they arrive at different points in the code.. In music, when one section ends, it leads to another. And the sections can contain even smaller sections. How music and code is organized is similar.
Hierarchy
- Smallest Level. Both music and code are made up of smaller bits. They gain their meaning often through context. In music cognition, which is a growing field, there is the concept of hierarchy. When you hear one note, you only hear the note. If you hear repeated notes, then the brain starts to process the concept of rhythm. Similarly one line of code might not mean much. We might have undefined variables, or we might have a function that does nothing. It’s only when we group the code in a way that it makes sense that it begins to have real meaning.
- Medium Level. In music we call small bits of music made of just a few notes motives. These motives are often reused, maybe transposed and sometimes altered. When we have DRY code that functions well and is very reusable this is similar.
- Larger Levels. Here in music we have whole melodies that take us from one chord to the next. Alone, the chords do not mean much. But together in context, they start to give us a sense of key or pitch center. Maybe we have a whole section of music that is our “A” section of a binary or ternary form (not to be confused with ternary operators!). In our code we have whole functions and classes that executes something just as a whole section of music is rendered.
- Largest Level. Here we have the whole artwork or the whole application which is made up of smaller sections. In a classical form, these might be sections in a binary or ternary form. In a React app, we might have our main app component, which renders other components within it , which also renders smaller components etc.
Other Parallels
Almost certainly there are more areas where music and programming intersect. Obviously applications for musicians have existed for years. I have personally used music notation programs such as Finale and Sibelius as well as recording and production software such as Pro Tools and Cubase. Can you write music with actual code directly? Perhaps. It is not one of my interests. A picture of a trombone is a visual artwork, not a musical artwork after all. To me, designing music with code feels a lot more like code than music. I think most of the great innovations with code and music reside in audio plugins for compression, reverb, eq, and things like that which are made digitally. Using code to manipulate sound is fascinating. The composer in me will always want to write music the old-fashioned way though. The code cannot read my mind after all…unless I write it! At that point why not just write the thing without code though!
At any rate, I hope this article sheds some light on most of the parallels between music and code. I hope to write more on the subject in the future, but hopefully this gives some general insight to those who are curious. In the meantime, I highly recommend learning React and listening to Poulenc’s Concerto Pour Deux Pianos!
📝 Read this story later in Journal.
🗞 Wake up every Sunday morning to the week’s most noteworthy Tech stories, opinions, and news waiting in your inbox: Get the noteworthy newsletter >