Translation of the article If Hemingway wrote JavaScript .
This article was written by my good friend and colleague, Agnus Kroll. Agnus works in the development team for the main functionality of Twitter, participates in various conferences and maintains a terrific JavaScript blog . In addition, he is a big fan of fiction, and I thought it would be cool if he wrote something about programming from a literature point of view.I loved fiction long before I wrote my first line of code. Now I write quite a lot on JavaScript, and, in addition, I also write a book about it.
')
So what is it about JavaScript that attracts so many literature fans? I have some stupid theories related to the expressive potential of limited syntax, but more on that some other time. Let's take a look at great writers: what if they wrote in JavaScript? As a Hemingway fan with experience, I would say that he would most likely have hated programming (and programmers). But I’m willing to bet that among the general contempt there would be a little warm spot for JavaScript, because that would be a kind of language. Free and deceptively simple on the surface that hides the essence and drama beneath.
Ancestor of all review code

I recently had a dream in which I asked Hemingway and four more luminaries of the world of literature to write for me a small function that returns the Fibonacci sequence of a given length. Each writer decided to go his own way, but in general they did a good job:
Ernest Hemingway
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; }
There were no surprises. Only the most necessary. Not a single extra word. Not a single extra variable. This is not to say that the code is beautiful. He is a little pedantic - but that's the whole point of Hemingway as a writer. There is no need for thoughtful logic or expressive names for variables. The code is clean, simple, does what it should and nothing more.
Hemingway could not stand fools, so if you ask about a sequence of less than two numbers, he will simply ignore you. “I'm tired and this question is idiotic.”
William Shakespeare
function theSeriesOfFIBONACCI(theSize) {
The bard was seriously divided, but it could not be otherwise. Note that the comments (comments, not remarks and titles of acts) are composed of iambic pentameter, bipartite in size, in which the stress falls on every second syllable in the foot. In his plays, Shakespeare often adds a dramatic accent, deviating a little from the iambic pentameter, adding an extra syllable or bearing stress in a word. And it seems that he uses the same trick here. Keep it up, Will.
Andre Breton
function Colette(umbrella) { var staircase = 0, galleons = 0, brigantines = 1, armada = [galleons, brigantines], bassoon; Array.prototype.embrace = [].push; while(2 + staircase++ < umbrella) { bassoon = galleons + brigantines; armada.embrace(brigantines = (galleons = brigantines, bassoon)); } return armada; }
As the founder of surrealism, Breton believes that dreams are much more interesting than reality, and they form the basis of our creative endeavors. Nouns selected appropriate. Despite the fact that the work of Breton is easy to criticize, they are eternal, always cordial and beautiful - unconscious raising the depths of imagination.
Most likely, Breton called the function in honor of his ex, and the result was obtained with the help of a fleet of ancient ships. The code itself is distinguished by the characteristic elegant logic - Breton uses the comma operator to simultaneously shift the values of the elements of the array. Hats off, Andre.
Roberto Bolano
function LeonardoPisanoBigollo(l) { if(l < 0) { return "I'd prefer not to respond. (Although several replies occur to me)" }
If you have not read at least one book of Bolano in your life, you have lived it in vain. His books are wonderful: at the same time carelessly complex and charmingly naive, and the style of the narrative can be characterized by disarmingly charming honesty.
Remaining true to himself, Bolano spiced up his code with confessions of insecurity, confusion, and ignorance. The solution of the problem itself, and the solution is brilliant, is served as if in thought. Always intrusive, always doubting, Bolano with greater joy offers us a not very interesting and completely useless list of geniuses-mathematicians.
But in the code there are several more characteristic features of Bolano's creativity: a symbiosis of short and long paragraphs, the absence of semicolons (reflecting the absence of quotes in his works), as well as the explicit use of global variables, suggesting that each variable will be used more than once in subsequent parts .
Charles Dickens
function mrFibbowicksNumbers(enormity) { var assortment = [0,1,1], tally = 3, artfulRatio = 1.61803; while(tally++ < enormity) {
I'm not a big fan of Dickens, and for the most part I agree with the damn accurate assessment of Henry James:
“If we venture to define his literary personality, we will have to call him the greatest superficial novelist. We understand that such a definition puts him at the lowest level of the writing workshop he adorns, but we are aware of the consequences of our assumption. In our opinion, it would be a crime against humanity - to put Dickens on a par with the greatest novelists. Because he created nothing but an image. He added nothing to our understanding of human nature. ”- Henry James on Charles Dickens, review of the novel“ Our mutual friend, ”The Nation magazine, December 21, 1865.
Instead of conclusion
Whether it’s Douglas Crockford’s defense mechanisms or the narrow and narrow limitations of computer science textbooks, doctrine and dogma will always be enemies of good JavaScript code. Some programmers like rulebooks and patterns, which is why we have Java. The joy of JavaScript lies in the flexibility and the sheer number of possibilities it provides. Natural languages have the same properties. The best JavaScript writers and developers are those who are captivated by the language, who study and experiment every day to create their own style, their idioms and expressions.
That's all. I hope you enjoyed this nonsense.