How to fail function calls with undefined arguments with a one-liner (sorta) (#snippet)

I’ve been writing JavaScript for so long that I sometimes don’t see its quirks. To be fair, there are fewer quirks than ten years ago, but some things would still make great language additions. One thing that would make a lot of sen…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

I've been writing JavaScript for so long that I sometimes don't see its quirks. To be fair, there are fewer quirks than ten years ago, but some things would still make great language additions. One thing that would make a lot of sense is required function parameters.

Assume you have a function that defines required arguments. And you really really want to make sure the function is called with the correct arguments by throwing an error.

Here's what you might do:

function doSomethingWithAThing(theThing) {
  if (typeof theThing === "undefined") {
    throw new Error("theThing is required");
  }
}

That's quite a bit of code for such a simple thing.

Unfortunately, you can't do something like this in JavaScript ...

function doSomethingWithAThing(
  theThing = throw new Error("theThing Is Undefined")
) {
  // ...
}

... because JavaScript doesn't like it.

Peter Kröner published a handy snippet that he carries around from project to project. The post is in German, so here's the English gist of it.

First, define a fail function.

function fail(reason, ErrorConstructor = Error) {
  throw new ErrorConstructor(reason);
}

And second... use it. 🫣

By restructuring the code and transforming the error throwing portion into an expression, you can immediately throw in case a function is called without the required parameters.

function doSomethingWithAThing(
  theThing = fail('theThing Is Undefined')
) {
  // ...
}

What a beautiful sort of one-line workaround.

Thanks Peter!


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2023-01-29T23:00:00+00:00) How to fail function calls with undefined arguments with a one-liner (sorta) (#snippet). Retrieved from https://www.scien.cx/2023/01/29/how-to-fail-function-calls-with-undefined-arguments-with-a-one-liner-sorta-snippet/

MLA
" » How to fail function calls with undefined arguments with a one-liner (sorta) (#snippet)." Stefan Judis | Sciencx - Sunday January 29, 2023, https://www.scien.cx/2023/01/29/how-to-fail-function-calls-with-undefined-arguments-with-a-one-liner-sorta-snippet/
HARVARD
Stefan Judis | Sciencx Sunday January 29, 2023 » How to fail function calls with undefined arguments with a one-liner (sorta) (#snippet)., viewed ,<https://www.scien.cx/2023/01/29/how-to-fail-function-calls-with-undefined-arguments-with-a-one-liner-sorta-snippet/>
VANCOUVER
Stefan Judis | Sciencx - » How to fail function calls with undefined arguments with a one-liner (sorta) (#snippet). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/01/29/how-to-fail-function-calls-with-undefined-arguments-with-a-one-liner-sorta-snippet/
CHICAGO
" » How to fail function calls with undefined arguments with a one-liner (sorta) (#snippet)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2023/01/29/how-to-fail-function-calls-with-undefined-arguments-with-a-one-liner-sorta-snippet/
IEEE
" » How to fail function calls with undefined arguments with a one-liner (sorta) (#snippet)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2023/01/29/how-to-fail-function-calls-with-undefined-arguments-with-a-one-liner-sorta-snippet/. [Accessed: ]
rf:citation
» How to fail function calls with undefined arguments with a one-liner (sorta) (#snippet) | Stefan Judis | Sciencx | https://www.scien.cx/2023/01/29/how-to-fail-function-calls-with-undefined-arguments-with-a-one-liner-sorta-snippet/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.