If Hemingway Wrote JavaScript

A new book exploring the intersection of literature and code is a delightful romp through some of the Western world’s most celebrated writers and the imagined JavasScript they would have — could have — slung had they been so inclined.

A new book exploring the intersection of literature and code is a delightful romp through some of the Western world’s most celebrated writers and the imagined JavasScript they would have — could have — slung had they been so inclined.

Written and compiled from submissions by Angus Croll, an engineer at Twitter, If Hemingway Wrote JavaScript celebrates literature and code in equal measure.

In particular, Croll explores the literary achievements and style of his authors — Virginia Woolf, Franz Kafka and David Foster Wallace among many others — and then imagines the JavaScript they’d write to tackle specific problems. For Croll, JavaScript is the “most literary of computer languages,” and like literature it’s open to conceptual breakthroughs based on the imagination of those writing it.

Reading through the book, we’re reminded that “although [Jack] Kerouac claimed to dislike the period and mistrust the comma, he used both liberally.” Or, “by the time he wrote Ulysses, [James] Joyce had abandoned narrative  authority entirely, in favor of an urgent, in-the-moment stream of consciousness in which both narrator and protagonist relate disjointed scraps of ephemera that mirror the random, cluttered, ever-changing character of interior thought.”

Or, take this intro to Tupac Shakur:

It’s hard to reconcile the two identities of Tupac Amaru Shakur. One was cerebral, sensitive, and compassionate: an actor and poet in his early teens, a devotee of Shakespeare who addressed women’s struggles, child abuse, and poverty in his lyrics. The other was a violent, gun-toting embodiment of the gangsta rap movement: in and out of prison and sporting a “Thug Life” tattoo across his stomach, killed at the age of 25 by an unknown attacker’s bullet.

All of which is to say that like authors and their telltale innovations, some programming languages lend themselves to a wide degree of stylistic expression.

Via If Hemingway Wrote Javascript:

JavaScript has plenty in common with natural language. It is at its most expressive when 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…

…Natural language has no dominant paradigm, and neither does JavaScript. Developers can select from a grab bag of approaches—procedural, functional, and object-oriented—and blend them as appropriate. Most ideas can be expressed in multiple ways, and many JavaScript programmers can be identified by their distinct coding style.

The book is divided into five “assignments” with each devoted to a few authors producing code to solve a given problem. The first assignment is to write a function that “returns the first n numbers of the Fibonacci sequence.” The Fibonacci sequence, in turn, is a series of numbers where each is the sum of the previous two. For example: 0, 1, 1, 2, 3, 5, 8, 13, etc.

Croll gets things started with the book’s namesake, Ernest Hemingway, whose prose, we’re reminded, “is never showy, and his syntax is almost obsessively conventional. The short, unchallenging sentences and absence of difficult words add a childlike quality to his cadence. He assumes the role of naive observer, all the better to draw his readers into the emotional chaos beneath.”

Here’s what Hemingway’s JavaScript looks like as he tackles the Fibonacci assignment:


function fibonacci(size) {

     var first = 0, second = 1, next, count = 2, result = [first, second];

     if (size < 2)
          return "the request was made but it was not good"

     while (count++ < size) {
          next = first + second;
          first = second;
          second = next;
          result.push(next);
     }
    
     return result;
}

Short. Concise. To the point. Or, in Croll’s analysis:

Hemingway’s Fibonacci solution is code reduced to its essentials, with no word or variable wasted. It’s not fancy — maybe it’s even a little pedantic — but that’s the beauty of Hemingway’s writing. There’s no need for elaborate logic or showy variable names. Hemingway’s JavaScript is plain and clear, and it does only what is necessary—and then it gets out of the way to allow the full glory of the Fibonacci sequence to shine through.

William Shakespeare comes next. Here’s the bard tackling the same problem:


function theSeriesOfFIBONACCI(theSize) {

     //a CALCKULATION in two acts
     //employ'ng the humourous logick of JAVA-SCRIPTE

     //Dramatis Personae
     var theResult; //an ARRAY to contain THE NUMBERS
     var theCounter; //a NUMBER, serv'nt to the FOR LOOP

     //ACT I: in which a ZERO is added for INITIATION

     //[ENTER: theResult]

     //Upon the noble list bestow a zero
     var theResult = [0];

     //ACT II: a LOOP in which the final TWO NUMBERS are QUEREED and SUMM'D

     //[ENTER: theCounter]
    
     //Commence at one and venture o'er the numbers
     for (theCounter = 1; theCounter < theSize; theCounter++) {
          //By divination set adjoining members
          theResult[theCounter] = (theResult[theCounter-1] || 1) +
          theResult[Math.max(0, theCounter-2)];
     }


     //'Tis done, and here's the answer
     return theResult;

     //[Exeunt]
}

Shakespeare, as Croll reminds us, played with the iambic pentameter popular in his time by deviating where stressed syllables lay and sometimes adding an extra syllable as well. His JavaScript doesn’t hesitate to do so either.

Notice that although Shakespeare’s comments are in iambic pentameter, he’s using weak endings (that is, adding an extra unstressed syllable). Shakespeare frequently used weak endings to denote enquiry or uncertainty (the Elizabethan equivalent of upspeak).

If Hemingway Wrote JavaScript should delight the nerdier fans of both literature and code, satisfying each whether they’re familiar with the authors or the inner workings of JavaScript.

As Croll writes, part of his effort is to bridge the gap between the humanities and computer science. The result, this book, is amazingly well done.

Bonus: The illustrations are great too.

This review originally appeared on The Future Journalism Project.

Thoughts? Ideas? Comments?
Send me a note or reach out on Mastodon.