Created
October 20, 2012 07:48
-
-
Save shesek/3922578 to your computer and use it in GitHub Desktop.
Throwing early errors for unexpected empty values
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 characters
# A quick way to assert that you get a non-null-or-undefined value when you don't expect ONE, and throw early | |
# errors when it does happen, rather than letting that value go elsewhere. | |
# Just a simple function that returns the value when its non-null-or-undefined, or throws an error otherwise.. | |
non_empty = (v) -> v ? (throw new Error 'null or undefined encountered where it shouldn\'t') | |
# (the extra parenthesis are there to make CoffeeScript compilation only execute an IIFE when we're throwing) | |
# Than, simply: | |
user_id = non_empty user.id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment