author profile
Bjørn Jarle Kvande

Fail early

Instead of accepting bad input and handling it, you should fail and throw an exception to force the fault up to the surface. If the bad input is accepted, the error might not be found for a long time.

The function below is trying to be a good guy and handle bad input. You might think this is robust, but it is not a good strategy as you will "swollow" errors from the higher ups.

export function isDateInTheFuture(timeStamp) { if (timeStamp != null) { return timeStamp > } else { return false } }
Instead you should be strict and angry and accept no crap. Let the caller know they are misbehaving by throwing an exception. export function isDateInTheFuture(timeStamp) { assert(isNumber(timeStamp), "Invalid timeStamp") return timeStamp > }
© Bjørn Jarle Kvande | Trailguide AS | ObjectPlanet AS
1998 - 2021
All rights reserved