Posted over 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.

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:

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

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

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:

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:

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:

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

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

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

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

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

With CoffeeScript

CoffeeScript lets you write:

x ?= 'default-value'
Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Henning Koch
Last edit:
5 months 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