November 26, 2015
November 25, 2015

Pure JavaScript vs. lodash, and the importance of decision-making in programming

Xkcd on the truths behind efficiency.

Don’t get me wrong — I love lodash. It has many useful helper functions, and I use it nigh on daily. But in many cases, it simply isn’t needed.

I recently opened a pull request in which i referenced to the first index of an array using the vanilla [0] notation. I didn’t think much it, to be honest. In the subsequent code review, I received the following comment (paraphrased):

“Why don’t you use the more readable _.first instead?”

The suggestion soon received support from a second reviewer. I must admit that, at first, I considered the comment to be nothing more than nitpicking. I wanted to know why using _.first would be a better option.

“Using _.first makes the code more readable.”

I disagree. C-style syntax is the most popular form of syntax used in programming languages today; a programmer who doesn’t immediately recognise what arr[0] does is, well, doing it wrong. For people unfamiliar with lodash/Underscore, however, _. may not be obvious[1].

So, to my eye, arr[0] is, at the very least, just as readable as _.first(arr). It’s also shorter, which is always a plus. But we’re missing a key issue: what about performance? Readable code is great, but efficient code is even better.

Here’s a small snippet of pure JS that refers to the first index of an array arr in a for loop that is executed 1,000,000 times:

function calculateSpeed() {
  var arr = [1,2,3,4,5];
  var start = new Date().getTime();
  for(var i = 0; i < 1000000; ++i) {
  var end = new Date().getTime();
  console.log(end - start);

I ran the code in Safari using the highly scientific “do it twice” method, getting a result of three milliseconds for both runs. Executing the code with _.first(arr); instead of arr[0]; gave a result of 24 milliseconds (both runs). That’s almost one order of magnitude worse. It may not matter much for typical use cases, but small inefficiencies pile up. Shit’s important.

So there we have it. My recommendation in this case is to prefer [0] over _.first. But, despite the lengthy exposition, this isn’t why I wrote this post. The review got me thinking about programmers’ reasoning and decision-making processes in general. Programmers make countless small decisions every day, and collectively, they determine the quality of the result. There are scores of complex decision-making models, but I find that using a mental checklist is usually good enough.

Checklists should be context-specific, but I find that a general checklist that focuses on readability, conciseness, performance and reusability serves as a good starting point. Given two competing solutions A and B, I ask myself these questions:

  • Is the code for A at least as readable as that of B?
  • Is the code for A at least as concise as that of B?
  • Is the code for A at least as efficient as that of B?
  • Is the code for A at least as reusable as that of B?

What are the questions you ask yourself? What does your mental checklist looks like? If you don’t have one, I highly recommend giving it some thought.

For my checklist, if the answer to all questions is yes, then I know what to do: Fire up a browser, open the pull request, and tell those pesky code reviewers that they are wrong.

  1. Okay, I admit that lodash and Underscore aren’t exactly obscure libraries, but you get the point. []
November 24, 2015

Mapping the United Swears of America

Helldamn and bitch are especially popular in the south and southeast. Douche is relatively common in northern states. Bastard is beloved in Maine and New Hampshire, and those states – together with a band across southern Arizona, New Mexico, and Texas – are the areas of particular motherfucker favour. Crap is more popular inland, fuck along the coasts. Fuckboy – a rising star* – is also mainly a coastal thing, so far.

Who knew?

Markov and You

A classic Coding Horror piece on Markov chains (text chains in particular).

November 23, 2015

Dance to Calypso

An insightful piece by Matt Mullenweg on the balance between maintaining a popular piece of software and adapting to advancements in technology.

Oh, and there’s this, too:

So we asked ourselves a big question. What would we build if we were starting from scratch today, knowing all we’ve learned over the past 13 years of building WordPress? At the beginning of last year, we decided to start experimenting and see.

Today we’re announcing something brand new, a new approach to WordPress, and open sourcing the code behind it. The project, codenamed Calypso, is the culmination of more than 20 months of work by dozens of the most talented engineers and designers I’ve had the pleasure of working with (127 contributors with over 26,000 commits!).

This is precisely the direction I had hoped WordPress would take: a JavaScript frontend (or frontends), communicating with the solid WP core over well-defined APIs.

November 21, 2015

TIFU by using Math.random()

Hands down the one of the best articles I’ve ever read on pseudo-random number generators, and the problems with V8’s implementation in particular. A complicated topic explained in easy-to-read language.

November 18, 2015

The Oxford Dictionaries Word of The Year 2015

It’s 😂, the “Face with Tears of Joy” emoji.

This year Oxford University Press have partnered with leading mobile technology business SwiftKey to explore frequency and usage statistics for some of the most popular emoji across the world, and 😂 was chosen because it was the most used emoji globally in 2015. SwiftKey identified that 😂 made up 20% of all the emojis used in the UK in 2015, and 17% of those in the US: a sharp rise from 4% and 9% respectively in 2014. The word emoji has seen a similar surge: although it has been found in English since 1997, usage more than tripled in 2015 over the previous year according to data from the Oxford Dictionaries Corpus.

I know the choice has received its fair share of flak on account of it not being a word but a pictograph, but I like it. Languages evolve naturally and it’s refreshing to see this acknowledged.