WordPress has been using “Code is Poetry” as their tagline since as long as I can remember using it (if anyone knows where it came from, I’d love to know).
The slogan has always been very effective at communicating the approach that WordPress takes, a blend of elegant simplicity and infinite expansion. All that lies behind your WordPress install is bits of code strung togehter like a symphony. Still, the phrase prompts a deeper look.
I am still no expert, but I have learned the basic principles of code and the process has really opened up for me. I have found more and more that code really does mirror poetry.
The Poetics of Coding
A couple of years ago, Matt Ward wrote an excellent article for Smashing Magazine dealing with this very issue. In it, Matt compares the structure, semantics and meanings of code and poetry. He rightly comes to the conclusion that just like poetry, code embraces the constraints of its form to create a meaning that is both purposeful and consice. He espeically emphasizes the importance of being structured in our code writing.
Like a haiku, or sonnet, programmers follow a set of standards that lead them to builds that not only follow a purposeful structure, but depend on them to function properly. For those that don’t believe him, you need only to turn to Nicholas Gallagher’s Idiomatic CSS Guide to understand how welcome structure and meaning is to good programmers.
Still, I would like to probe this idea a bit further. Let’s take a look at one of the most basic code blocks you’ll ever see in WordPress: The Loop. All loops begin and end the same way (let’s ignore query posts for now). Let’s take the basic Loop from the brand new Twenty Twelve homepage modified a bit for simplicity.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post; ?>
<?php get_template_part( ‘content’, get_post_format() ); ?>
<?php endwhile; else: ?>
<p>Sorry buddy, no posts here</p>
<?php endif; ?>
We’ll pull in all of our posts, in default order, and call the content template to handle what information to bring in. Nothing too special.
Now let’s take a look at a poem from 1852 by Emily Dickinson, a poet known for her adherence to form and understated beauty.
During my education
It was announced to me
That gravitation, stumbling,
Fell from an apple tree!
The earth upon an axis
Was once suppossed to turn,
By way of a gymnastic
In honor of the sun!
Structurally there are definitely similarities. When writing code, a good programmer knows to indent a line in order to show a child hiearchal connection to the line that precedes and follows it. Parenthetical statements indicate that a function can be unpacked and extended, thus giving it more a more select purpose.
In the Dickinson poem, every other line is indented, so a sort of echo and response heiarchy is given to the words. Italics are used for emphasis, a rough highlight as your eye passes them by, giving them a special meaning, just like the parentheses in our code. See, this is easy!
But deeper down the rabbit hole we go. Read the Dickinson poem out loud. One of the nice things of well written poetry is that it has a flow. Everything just feels like it is in the right place. As you read through, your words jump naturally from one line to the next. When we are coding, this should be our goal as well.
Each line connects to the one above it, and bridges it to the next. A good programmer can read through a well crafted Loop and feel that same sense of kineticism. Poetic flow is different from prose because it is not determined simply by grammatical structure, but by the way each word naturally bounces to the next, unveiling something more essential through their juxtapositon.
Code has the same effect; each function, each coding phrase infuses with the words around it to create meaning. Each line, juxtaposed with the next, is able to do something that neither could do on its own. But they must connect naturally, and flow into one another. Sounds poetic, right? Well, we’re not finished.
Microscopic Bits of Art
Perhaps this idea can be better explained by comparing code to another art form, typography. In The Elements of Typography, Robert Bringhurst, in a brief moment of reflection, compares the work of typographer to the work of programmers.
But the bits of information handled by typographers differ in one essential resepct from the computer programmer’s bits…. Letters are microscopic works of art as well as useful symbols. They mean what they are as well as what they say.
Now, I think The Elements of Typography is a great read, but this is an opinion that I just can’t agree with. In fact, the exact opposite is true. Just like typographers, programmers deal with indexical symbols that we piece together as logically as we can.
Like the letters of Bringhurst’s prose, these microscopic bits of art have meaning all on their own, be it a function, a conditional statement or even a HTML header tag. But the beauty of these symbols comes when they are assembled togehter into a meaningful structure. A process that sounds simple, but is in fact wonderfully complex.
The Poetic Process
Listen, real poetry doesn’t say anything; it just ticks off the possibilities. Opens all doors. You can walk through anyone that suits you.
You will probably hear more then once in your life that design is all about solving problems. Well it turns out that so is code. For each problem we may face, there are likely dozens of possible solutions. So much so, that it can often be overwhelming. It’s easy to rush into getting started, and open up a dozen tabs with different Google searches all of which suggest a different solution.
But instead, embrace the poetic process. Think about the realm of possibilities that lie before you, and choose the one that feels most natural. Before you begin, choose which door you are drawn to. It will be much easier to walk through.
Let’s look at another poem to understand the outcome of this process. This is one stanza from “Ars Poetica” or the The Art of Poetry, by Archibald MacLeish, written in 1926.
A poem should be equal to:
For all the history of grief
An empty doorway and a maple leaf.
The leaning grasses and two lights above the sea—
A poem should not mean
Good poetry is exactly as he describes. It simply exists; not transparent exactly but wholly communicative and visceral. And of course, MacLeish’s stanza is a good example of this. It leaves you with a feeling and an understanding, without you even being aware of the limitations of structure, or the constraints of the medium. Simply put, good poetry feels as if it should exist exactly the way it does and could not any other.
When writing code, we should aim for the same sense of lightness. Coding standards exist for a reason, and if you follow them, you will find that you will not only achieve a beautiful poetic flow, but code won’t even feel like code. Instead, it will be a sort of opaque expression and inspire an implicit understanding even for someone who has never seen a line of code in their life.
One of my favorite examples of this is from WP Daily’s own Tom McFarlin in his Plugin Boilerplate. This is not only because it is meant as a teaching tool, but because it doesn’t try to cheat or use workarounds or make it’s way outside of structure.
Instead, this snippet follows the rules and communicates exactly what it wants to at every step. I’ve removed the documentation from the snippet, because I want you to truly try to read it just as you would poetry, because to me they are sometimes one and the same.
So now, when you go to sit down to write your own code, consider flow, consider the significance that lies between the lines. Work towards connecting microscopic bits of art to build something that not only proposes a solution, but creates meaning in it’s own right.
And remember always that in the best examples, code really is poetry.