Last active
July 18, 2021 11:42
-
-
Save MarkTiedemann/17ebf5b38b5b7566478de7f623e1defd to your computer and use it in GitHub Desktop.
Revisions
-
MarkTiedemann revised this gist
Jun 17, 2016 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -70,12 +70,12 @@ const foo = (mustBeProvided = x`mustBeProvided`) => {} foo() // ==> Error: Missing parameter: mustBeProvided ``` **Simple "real world" example using [`throw-if-missing`](https://github.com/MarkTiedemann/throw-if-missing):** ```js const x = require('throw-if-missing') const login = ({ username = x`username`, password = x`password` } = {}) => {} login({ username: 'C-3PO' }) // ==> Error: Missing password ``` -
MarkTiedemann created this gist
Jun 17, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ # An Easier Way to Enforce Required Parameters in ES6 **Expands on [Handling required parameters in ECMAScript 6](http://www.2ality.com/2014/04/required-parameters-es6.html) by [Axel Rauschmayer](https://twitter.com/rauschma).** The idea (which is credited to Allen Wirfs-Brock) is, in essence, to use default parameter values to call a function which throws an Error if the parameter is missing: ```js const throwIfMissing () => { throw new Error('Missing parameter') } const foo = (mustBeProvided = throwIfMissing()) => {} foo() // ==> Error: Missing parameter ``` This idea is great. However, the Error message does not tell you which parameter is missing, which could be quite helpful if the function had multiple parameters or an option object with multiple options, for example. To give more accurate Error feedback, we could refactor the code as follows: ```js const throwIfMissing = p => { throw new Error(`Missing parameter: ${p}`) } const foo = (mustBeProvided = throwIfMissing('mustBeProvided')) => {} foo() // ==> Error: Missing parameter: mustBeProvided ``` Now, the code for such a seemingly simple task, enforcing that a parameter is required, has become quite long, however: minus the parameter name, `throwIfMissing('')` is 18 characters long. Since this is such a fundamental and often used task, it makes sense to make it as short and easy-to-type as possible. If we use template literal syntax `foo´string´` for calling the function instead of the normal round brackets syntax `foo('string')`, we can already reduce the number of characters by 2. ```js const throwIfMissing = p => { throw new Error(`Missing parameter: ${p}`) } const foo = (mustBeProvided = throwIfMissing`mustBeProvided`) => {} foo() // ==> Error: Missing parameter: mustBeProvided ``` Nevertheless, it is still 16 characters long, excluding the parameter name, of course. Now, the only option left to reduce the number of characters needed for this simple task is to rename the `throwIfMissing` function. There are quite a few shorter alternatives which I like, for example: - `throwIfNo` (e.g. `throwIfNo´password´`) - `enforce` (e.g. `enforce´password´`) - `ensure` (e.g. `ensure´password´`) My favorite, however, is just `x`. Why an `x`? - Well, the `x` represents a check mark: `[x]`. - It's also super short. Now, we're from 18 characters down to 3 (plus the parameter name). **Before:** ```js const throwIfMissing () => { throw new Error('Missing parameter') } const foo = (mustBeProvided = throwIfMissing()) => {} foo() // ==> Error: Missing parameter ``` **After:** ```js const x = p => { throw new Error(`Missing parameter: ${p}`) } const foo = (mustBeProvided = x`mustBeProvided`) => {} foo() // ==> Error: Missing parameter: mustBeProvided ``` **Simple "real world" example:** ```js const x = o => { throw new Error(`Missing option: ${o}`) } const login = ({ username = x`username`, password = x`password` } = {}) => {} login({ username: 'C-3PO' }) // ==> Error: Missing option: password ```