A quick romp through default values in CoffeeScript

This was last updated for CoffeeScript 1.7.1.

Imagine that this sentence is a beautifully-crafted, flowing intro paragraph. "Wow, what a great introduction to this CoffeeScript guide," you whisper. Now, let's quickly explore all of CoffeeScript's ways to set default values.

Before you start, make sure you know about undefined, null, and falsy values in JavaScript. Check this out if you need help.

As a function parameter

To quote the CoffeeScript documentation, functions can have "default values for arguments, which will be used if the incoming argument is missing (null or undefined)." Here's how you might use it:

shootLaserBeam = (color = "red") ->
  # ...

Now, if the first parameter is undefined or null, it'll be defined as the string "red". If it's anything else (even falsy values like 0 and the empty string), it won't be set to the default value.

Using the existential operator

CoffeeScript's existential operator can be used to set default values.

sendThreateningMessageFromUFO = (options) ->
  options.message ?= "I will hold your pathetic planet hostage"
  # ...

You can also set another variable using a sexier version of a ternary operator.

# this...
message = evilMessage ? "Exterminate!"

# ...is equivalent to this:
if evilMessage?
   message = evilMessage
   message = "Exterminate!"

# ...or this:
message = if evilMessage? then evilMessage else "Exterminate!"

The ternary operator makes sure that the variable isn't undefined and isn't null.

You'll get a compile error for if you try to define not-yet-defined variables. For example, this gives a compiler error:

someUndefinedVariable ?= "I will destroy your worlds"

This makes sense, right? Why set a default value for something that's totally undefined? If you, for some reason, need to do this, you can use the existential operator on the variable:

someUndefinedVariable = "Flee, puny humans" unless someUndefinedVariable?

Discarding falsy values, too

If you want to set defaults for any falsy value (not just undefined and null), you can use or=.

angryAliens = false
angryAliens or= true
# angryAliens is now true

# ||= is totally equivalent to or=
ufoCount = 0
ufoCount ||= 50
# ufoCount is now 50

# compare it to the existential operator:
numberOfAliens = 0
numberOfAliens ?= 1000
# numberOfAliens is still zero

The important takeaway here: ?= only discards null and undefined, where or= filters any falsy value.

For undefined only

Most of CoffeeScript either tests against falsy values or "is it null or undefined". You might want to set things only if it's undefined and nothing else, not even null. Here's how you might do that:

cropCircleCount = 69 if cropCircleCount is undefined

Now, if cropCircleCount is null or anything falsy other than undefined, it'll stay that way.

While this isn't as built-into-the-language as some of the features above, the fact that it's on one line is a little nicer than regular JavaScript. undefined is an undefined keyword in ECMAScript 3 and below. This means that this JavaScript fails on older browsers:

var isUndefined = window.myFunVariable === undefined;
// This fails on older browsers because "undefined" is, well, undefined.

CoffeeScript makes sure that this works in older browsers (by compiling undefined to void 0 to reliably produce an undefined value).

A summary

And now, a summary.

  • Use default parameters if it's a function parameter. Only sets to default if the value is null or undefined.
  • Use ?= to set a value to a default if the value is null or undefined.
  • Use or= (or ||=) to reject other falsy values like 0 and the empty string.

Go forth and set your CoffeeScript defaults, friend.

Wearable Tech or Technical Wear?

From "Wearable Tech or Technical Wear":

I'm excited for the Glass-Luxottica joint task force. It's a step away from futuristic looking wearable tech and a step towards technological wearbles for which I would dish out that dolla. If wearables are designed to be seamlessly and beautifully integrated into everyday life with all of the consumer base in mind—yes, even the omniscient white male—then I believe Wired's prediction that wearable tech being as big as the smartphone could come true.

I agree; wearables are currently being made for nerds, not for fashionable people.

Install Node Version Manager without the source line

I like the Node Version Manager for, well, managing Node.js versions. When you install it, it adds a source to the bottom of your profile (effectively source nvm.sh). I didn't want it to do that during installation, though. I found myself pondering, "How do I disable that?"

It's pretty easy, it turns out; you just tell it to redirect that source line elsewhere, like so:

curl https://raw.githubusercontent.com/creationix/nvm//install.sh | PROMPT=/dev/null sh

The install script looks for a variable called "PROMPT" and adds the source line to that, so setting it to /dev/null adds the line to nothing!

You could also install it by cloning it from Git and doing it manually, but I prefer this because I can be sure I'm getting a stable version without doing a lot of commands.

About Feminism

From About Feminism:

We happen to be humans who loved technology and embrace it wholeheartedly. But it was always clear things would be different if we were male.

I don't think I say "must-read" too often; this is a must-read.

Writing the code was typically less than half the problem

From an article about Valve's design process:

It became very clear the technology itself was only a small part of the work and integration, training, and follow-through were absolutely necessary to make the technology useful to the game. Writing the code was typically less than half the problem.

An echo of the idea that technology by itself does not transform anything.