How to Write Code like a Poet

Although created by opposite sides of the brain, coding and poetry are not that different. Hear me out.

Poetry is a form of art; a way for a poet to express sensations in a creative fashion that can be kept and referenced as a sort of societal pamphlet, giving readers insight into the lives of the poet. Fundamentally, however, poems function as art, characterized by linguistic beauty and met with a keen eye that looks not for anything in particular except meager satisfaction.

On the other hand, or perhaps merely a few fingers away, code is also a form of art; a way for a programmer to express cool and hard logic in a creative fashion that can be kept and referenced as a sort of algorithmic pamphlet, giving computers insight into their own value. Fundamentally, however, programs function as tools, characterized by the tireless search for efficiency and met with a keen eye that looks only to reduce the time spent on a task.

There are parallels between them that go beyond the presence of english characters in each. Both act as a means to bring about change in the world, poetry interacting via politics, programs via data, and yet at large it is thought that they diverge where poetry is almost meant to be beautiful, and where code is not.

Code, however, is accompanied by a characteristic called elegance, one that is fashioned by the beautiful art of optimization, and fueled by the backbone of poetry; creativity. Just as a genius poet might consider all literary techniques a thesaurus can handle, and weave them together into a directed masterpiece, a programmer is given a similar toolset in order to most efficiently, effectively, and beautifully, perform some task.

Elegant code is not code golf, where competitors seek to complete tasks in the shortest possible number of characters (see here), and nor is it merely code that functions. Elegant code is readable, takes a complicated problem and solves it simply, and is a solution that seems so obvious and genius and intricate in hindsight, and is coupled with the sense of satisfaction equatable to finally understanding a poem.

But what-ho! You say. That’s not poetry, because in poetry any arguable interpretation is valid (see Perrine). Code isn’t arguable!

And that’s absolutely correct, code executes the exact same way, every single time. Same input, same output. Every. Single. Time. Right?

Wrong!

Technically… And of course being technically correct is the best kind of correct, or so a programmer would say (and maybe a poet).

Formally, that is a correct statement. Computers can only operate on ones and zeroes, and everything outside and in between is merely some combination of the two. However, a very useful stipulation called random numbers makes programs have the capacity to be much more fluid and natural. Well, again… technically.

Randomness in computers is actually pseudorandom, meaning usually it involves hashes — here referring to values that may or may not be consistently passed — passed to some arbitrary evaluator. Usually this means taking something that changes about the computing environment, such as an internal clock or atmospheric noise, and scrambling it with some trigonometric functions to receive a “random” input.

Randomness is actually quite prone to elegance in code, and a lot of the modern frontier of computer science focuses on just how useful randomness is. The fundamental use of random values is to check cases arbitrarily when there are too many cases to work through, which leads to a lot more applications than one might think, and a lot of them come up in the pride and joy of programmers: sorting algorithms.

Quicksort is an algorithm that selects “pivot points” within a dataset and partitions the data into two sets based on whether the element is greater than or less than the selected pivot point. When computed recursively, meaning that the same process is repeated on each partition until each partition has a length of one, and thus cannot be split, the sorted set will be a combination of remaining elements and the set of partitions. This process, in big O notation, which is a count of total iterations, has a length of the number of items squared, or n^2, which for sets greater than tens of thousands is incredibly slow. Pseudorandom partition selection has an outcome of, on average, n log n, which is incredibly more efficient than without it, and requires less auxiliary space than merge sort, a similar n log n recursive sorting algorithm. It is in that mischievous way that randomness can be elegant.

We’ve talked a lot about the elegance of code, but what does that have to do with poetry? How do you write code like a poet?

Truthfully, elegance has nothing to do with it.

Make it work beautifully and you’ve got yourself a flying chance at art.

3 thoughts on “How to Write Code like a Poet”

  1. Have you heard of the programming language Shakespeare?
    https://en.wikipedia.org/wiki/Shakespeare_Programming_Language

    I like how you say that there are parallels between art and poetry, since both bring about change in the world. Your post reminds of a vsauce video since it seemingly effortlessly transitions between various topics.

    How is code golf not elegant? As is often said “make it as short as possible, but not any shorter”!

    Maybe if you had a quantum computer you could obtain true randomness. Speaking of sorting algorithms, my favorite sorting algorithm is bogosort … What language is the last picture of yours written in? I’m curious.

  2. I love this comparison, and I think your blog was really insightful. I think you have a good point that extends more generally, that science and art are more connected as we think and are really the same. There is an art to science in the ways we think creatively to solve problems and in the beauty in nature. There is science to art in the ways we think strategically to set up a poem or line up a photo, and the ways some aesthetics appeal more to the mind than others. I think that both coding and poetry can be considered the same in that they use lines and specific language (Java, metaphors, rhyme, Python) in order to express a certain message (a theme, an algorithm). The literary devices a poet uses, which they choose and think through carefully, mirrors the way a programmer chooses a language and each step carefully for their purpose. A poem is almost like a code for people, sending the message and sometimes urging us to perform an action or live a certain way much like a code tells a computer how to act. While I don’t know a lot about coding, I know that both code and poetry require creativity, problem solving, and the need to send a message.

  3. I liked the word elegance to describe well-crafted code. I agree with the idea that the best code is one that is the most efficient, effective, and beautiful to look at. There’s something about well-formatted code with concise steps that makes it much attractive. I was intrigued by your statement that “Elegant code is not code golf” and I can kind of understand why you would say so. When you introduced me to code golf, it was a very different coding experience, as you would need to search for different programming languages that could take functions and compress it into a single character. It results in very efficient code, but it doesn’t have that aesthetic aspect of sustenance and purpose. This was an insightful blog into explaining that code is more than just 0s and 1s, it’s a work of art.

Leave a Reply

Your email address will not be published. Required fields are marked *