When writing code one of our goals should always be to catch errors as early as possible.
If our tools / code exist in a way that means we don’t catch all or most errors until runtime…then we have serious flaws in our approach.
Take TypeScript for example. If we actually use this tool right we can catch so many errors during dev/ compile time.
Make sure you are:
- using strict mode
- use generated types from your service layer (single source of truth)
- avoiding casts unless absolutely necessary
- absolutely no using the unknown double cast
- no using // @ts-ignore suppressions
- compile your type code on each local commit
- compile your type code on CI runs before merge
And for errors that may still creep their way through to runtime, make sure your code is robust enough to handle these (null guards, try-catch blocks, error boundaries etc) and leverage a tool like Rollbar to help create an audit of such errors so they can be addressed.
And never forget the “real dollars” saving if we catch errors before production runtime.