“If Hemingway Wrote JavaScript”: A creative programming exercise

Twitter engineer Angus Croll had a dream where he gave 25 literary figures an assignment to solve a common coding exercise with JavaScript, and, to his surprise, they were (almost) all able to write workable solutions. You can’t keep something like that to yourself, so Croll shares it with us in his book, “If Hemingway Wrote JavaScript.”

Though we tend to classify engineering as a science, it’s also an art. It takes a lot of creativity to come up with new designs, and everyone has their own style and way of expressing their engineering ideas. developers and great authors past and present may not seem like the closest pair, but they both flex their creative muscles to write in their given format – code or literature.

“JavaScript has plenty in common with natural language,” Croll writes. “It is at is most expressive with combining simple idioms in original ways; its syntax, which is limited yet flexible, promotes innovation without compromising readability. And, like natural language, it’s ready to write.”

He explains how developers, like authors, can take multiple approaches to writing code – such as procedural, functional, and object-oriented for programmers – and use them as they see fit. They can break rules to overcome obstacles and create new patterns, just like groundbreaking historical writers did to create their masterpieces.

Most of us are probably familiar with the styles and works of some of the big-name authors like Ernest Hemingway, William Shakespeare, Dan Brown, and Douglas Adams, but some are more obscure. No to worry, though, as Croll has brief introductions to get you familiar with each author’s background and style before jumping into the author’s completed coding assignment. Croll then explains how the author’s style is reflected in how they wrote their code and approached the exercise. Hemingway is plain and to the point, Dan Brown finds mysterious artifacts in his solution, Lewis Carroll takes us down the rabbit hole for an answer, and Shakespeare uses the form of a play and even an attempt at iambic pentameter.

There are also a few extras like a clever rewrite of “The Raven” about the internal struggle of programming, and one of Dylan Thomas’s “Do not go gentle into that good night” about code rewrites. There’s something for everyone. Croll explores classical lit through the modern day – even rapper and poet Tupac Shakur tries his hand at a coding problem – and a variety of coding challenges that are bound to be familiar to many developers.

Croll admits that some of the examples are a bit outlandish – Hemingway is probably the best example for real, practical coding, and Kerouac perhaps the worst ¬– but he hopes they can be inspirational and help foster deeper thinking about programming.

I enjoyed comparing familiar authors’ styles in the code, comments, and overall approach, though admittedly I’m a programming novice and may have missed much of the humor and plays on code a software developer would likely catch.

And, hey, it’s gift season – bring some fun inspiration to the software developer in your life.

Topics covered in this article