0

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.

Guilherme Oderdenge
  • 4,935
  • 6
  • 61
  • 96
  • To the mod who marked this question as duplicate, could you point me where the link attached answers my question? Sorry, but a thread with a similar title says nothing. Note: I already read most of that thread and couldn't find what I'm looking for. – Guilherme Oderdenge Dec 03 '21 at 16:16
  • 1
    Discussion [here](https://stackoverflow.com/questions/944592/best-practice-for-using-assert) and [here](https://stackoverflow.com/questions/33083168/node-js-should-i-keep-asserts-in-production-code). Prevailing wisdom is, in my opinion, for things that should *never* happen and that would represent a coding error that you should have found at unit/integration test, feel free to use assert; for things that can happen at runtime (e.g. if file not found or data is bad), throw an error. – jarmod Dec 03 '21 at 16:20
  • Now we're talking, @jarmod. Thank you for pointing those links. Curiously, I didn't find them while researching. Have a nice day! – Guilherme Oderdenge Dec 03 '21 at 16:21

0 Answers0