Created
October 24, 2018 09:11
-
-
Save casperin/30555cb3971b3b3503ee208c36db87d0 to your computer and use it in GitHub Desktop.
Using CONSTANTS in JS is not problem free
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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