“Poetry” and “code” are two words you would never imagine finding in one sentence. The practitioners of code have people breathing down their necks to finish that project which should’ve been finished yesterday –– poetry is definitely the last thing on their minds.
Poetry lovers on the other hand, look down upon code and consider it another monster of cold logic and reason which is so against their nature. But are code and poetry East and West and never the twain shall meet?
In school, a teacher once wrote a few lines on the board and asked us if it was poetry. I sensed a trap but replied that it couldn’t possibly be anything of that sort because the lines did not rhyme. The lesson which ensued opened my eyes and I realised that poetry isn’t about writing rhyming lines. I understood why I hated some “poets” –– their lines were rhyming no doubt, but that alone isn’t sufficient for poetry. Poetry wears different garbs and it takes some effort to recognize it.
As a greenhorn programmer I had heard of maxims like “code is read much more often than it is written” and scarcely understood what it meant. Then one day I had to figure out what was wrong with the code I had myself written a week back.
I was astounded. Was this Greek or was this Latin? I couldn’t make head or tail out of the thing because it was really shabby. The meaning of those sayings dawned on me. I’d like to say that I instantly changed my ways and and started writing great code. But in all honesty, it took conscious and extended effort for me to start writing decent code.
I maintain that what we tend to call ‘elegant code’ is code we know from experience to be good. Further, we should not ignore the aesthetics of the code but strive to write poetic code.
Consider this example,
Underlying this piece of code is a simple idea. The filter() method returns a QuerySet object, which allows you to chain the filter() methods together. It seems very simple, but in reality it takes a leap of logic to come up with this idea. This code is not only elegant, it is also flexible. It allows you to do this, for example:
All the while being easier to read and understand intuitively. Even someone unfamiliar with Django can understand what it implies.
“Poor Faulkner. Does he really think big emotions come from big words? He thinks I don’t know the ten-dollar words. I know them all right. But there are older and simpler and better words, and those are the ones I use.”Hemingway
Every programming language comes with a suite of “tricks” and “hidden features”. It can make you look cool and perhaps make your code shorter. These “tricks” are good for obfuscation contests, not for production code. In common with good writing, your goal is to convey your thoughts clearly without making the reader rush to the dictionary or the main page.
What has simplicity and elegance got to do with the readability of the code? The answer to this lies in what we think of as “beautiful” or “cute“. Those of us who write and read code day in and day out start to recognize patterns of good and readable code. We are wired to think of these desirable traits as beautiful.
Which is why we shouldn’t be so quick to dismiss the aesthetics of code as something to brush under the carpet while muttering “form follows function”. Programming languages are similar to human languages in not just name, but also in purpose. Which is why the code you write is a means of expression. As programmers, we should strive to write beautiful poetry and not gibberish.
“Upon a midnight dreary, while you ponder weak and weary”, do give a thought to how you can make your code look more beautiful.
At Compile.com, we deal with a variety of datasets both big and small. Often, there is a need to run analysis on top of 3rd party datasets that we haven’t ingested to see if it’s worth the effort. This particular …