Skip to content

Instantly share code, notes, and snippets.

@casperin
Created October 24, 2018 09:11
Show Gist options
  • Save casperin/30555cb3971b3b3503ee208c36db87d0 to your computer and use it in GitHub Desktop.
Save casperin/30555cb3971b3b3503ee208c36db87d0 to your computer and use it in GitHub Desktop.
Using CONSTANTS in JS is not problem free
/*
I've seen this happen more than once. The data structure changes
its implementation, and suddenly some other code that you forgot
to update fails silently.
Here, Constants used to have "TYPE" instead of "STATE". We
updated it, but forgot to update the user of thing.
*/
// thing.js
export const Constants = {
STATE_1: 'STATE_1',
STATE_2: 'STATE_2'
}
export const thing = {
state: Constants.STATE_1
}
// user-of-thing.js
import {thing, Constants} from './thing.js'
thing.type === Constants.TYPE_1 // true
thing.type === Constants.TYPE_2 // true 😱
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment