Posted about 2 years ago. Visible to the public. Repeats.

Don't use the || operator to set defaults

I often see the use of || to set a default value for a variable that might be nil, null or undefined.

Copy
x = x || 'default-value'

This pattern should be avoided in all languages.

While using || works as intended when x is null or an actual object, it also sets the default value for other falsy values, such as false. false is a non-blank value that you never want to override with a default.

To make it worse, languages like JavaScript or Perl have many more falsey values, including the number 0 or the empty string ('').

Setting defaults correctly: Ruby

Prefer to check for nil? before overriding a value with a default:

Copy
x = 'default-value' if x.nil?

If you're reading from an options hash, you probably want to use fetch:

Copy
x = options.fetch(:x, 'default-value')

fetch returns the default value only if the key :x is missing from the Hash entirely. Note that there is a difference between a hash missing a key :x, and a hash that has :x assigned to nil. In the last case, the hash still has a key :x, and fetch will return its value nil.

Setting defaults correctly: JavaScript

Plain ES5 (no libraries)

Prefer to check if the variable is null or undefined before overriding its value with a default:

Copy
if (x === null || typeof x === 'undefined') { x = 'default-value'; }

ES-next with Babel

There is also a stage 3 proposal for the "nullish coalescing" syntax:

Copy
x = x ?? 'default-value'

If you are using Babel, you can transpile this syntax into ES5 using the @babel/plugin-proposal-nullish-coalescing-operator package.

With Lodash

If you are using Underscore or Lodash you can also write this as:

Copy
if (_.isUndefined(x) || _.isNull(x)) { x = 'default-value'; }

If you are using a recent version of Lodash you can also write this as:

Copy
if (_.isNil(x)) { x = 'default-value'; }

This is also possible in recent Lodash versions (but slower if your default value is expensive to construct):

Copy
x = _.defaultTo(x, 'default-value')

With CoffeeScript

CoffeeScript lets you write:

Copy
x ?= 'default-value'

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Henning Koch
Last edit:
7 days ago
by Besprechungs-PC
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more