ES6 makes the Hokey Pokey easy

As I make my way through various tutorials, some topics stick out more than others. This is especially true for tutorials that cover change. These are different from those that are about a new (to me) language or framework. For those, almost everything is sparkly and sticking out like a neon porcupine. There are the ah-ha moments, short for “Ah-ha, so that’s why this framework came to be needed”, but that’s not the same ah-ha I get when I learn about improvements to something that exists.

I’m following Wes Bos‘s ES6 for Everyone and had to stop for a few days after the section on swapping variables with destructuring. My first reaction was THANK CHEESES! My second was “Srsly, it took them this long to find their inner hatred of var temp?!” I always found this not only to be a pain for the extra lines but also for just the logic. I am all about logic puzzles like the River Crossing problem, but your code shouldn’t be a logic problem. It should be a logical solution.

Out with the old:

let footOut = "left foot";
let footIn = "right foot";
var temp = footOut;
footOut = footIn;
footIn = temp;
"That's no way to live your life!" - Wes Bos (my favourite Bosism)

I hated that. I can see it, but it doesn’t flow when I type it, and it’s never become automatic the way other things have (for-loops, named and anon functions, etc.). If I have to think about it every time, my time is wasted. I majored in German. I can get my head around change like shoving a conjugated verb to the end of a subordinate clause. I don’t need simplicity. I need speed, Ms. Mead. I need a flow, Moe.

Thus spake JavaScrusthra:

let footOut = "left foot";
let footIn = "right foot";
[footOut, footIn] = [footIn, footOut];

Short. Sweet. Complete. If this doesn’t matter to you, imagine writing an app for the Hokey Pokey. Too much? Then a function for shuffling.

Or don’t change at all. Stick with var temp = “Edsel”.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s