This is pretty common in codes of mine:
function doSomething() {
if (!value) {
throw new Error('Value is not set')
}
// ...
}
But that's quite exhausting and redundant to write. So one solution that I potentially found was using assert:
function doSomething() {
assert(value, 'Value is not set')
// ...
}
However, I never found assert
associated with code implementation, but mostly only during debugging and testing contexts. Of course, at the end of the day assert
is just a tool and we must use it if it makes our lives easier, but I am curious if it has any obscure something that using it in production is not advised.