Writing code certainly feels very similar to writing poetry. When I'm writing poetry, it feels like the center of my thinking is in a particular place, and when I'm writing code the center of my thinking feels in the same kind of place. It's the same kind of concentration. So, I'm thinking up possibilities, I'm thinking about, well, so how do I reinvent the code, gee, you know, what's the simplest way to do this.
I'm thinking about things like simplicity -- how easy is it going to be for someone to look at it later? How well is it fulfilling the overall design that I have in mind? How well does it fit into the architecture? If I were writing a very long poem with many parts, I would be thinking, "Okay, how does this piece fit in with the other pieces? How is it part of the bigger picture?" When coding, I'm doing similar things, and if you look at the source code of extremely talented programmers, there's beauty in it. There's a lot of attention to compression, using the underlying programming language in a way that's easy to penetrate. Yes, writing code and writing poetry are similar.
I think code is like a sonnet. There are rules you have to follow with sonnets. It must be fourteen lines long and certain lines have to rhyme The rhythm of each line has to be either di Dum di Dum (emphasis starting on the second syllable), or Dum di Dum di (emphasis starting on the first syllable). You have to end with a rhyming couplet. This one has emphasis starting on the second syllable, and it uses a rhyme scheme that looks this: ababcdcdefefgg.
William Shakespeare (1564-1616)
Sonnet XVIII: Shall I compare thee to a summer's day?
Shall I compare thee to a summer's day?
Thou art more lovely and more temperate.
Rough winds do shake the darling buds of May,
And summer's lease hath all too short a date.
Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd;
And every fair from fair sometime declines,
By chance or nature's changing course untrimm'd;
But thy eternal summer shall not fade
Nor lose possession of that fair thou ow'st;
Nor shall Death brag thou wander'st in his shade,
When in eternal lines to time thou grow'st:
So long as men can breathe or eyes can see,
So long lives this, and this gives life to thee.
Writing good code is a process of continually re-editing and refactoring. The rules are more general than sonnet rules. Never use a global variable if you can avoid it. Code should be clear and easy to read. Comment the code, test it, and document it. Write the simplest solution that gets the job done. Don't reinvent the wheel. Make it elegant, efficient and fast. Write something you're not ashamed for others to see. Code is poetry.