Invental/Review lessons/Code that silently does the wrong thing

Code that silently does the wrong thing

When 0 is valid, decide option precedence

When 0 is a valid value for an option, decide precedence explicitly (“if set, ignore the other option”) instead of patching truthiness checks one at a time.

01 · Before and afterThe pattern

TypeScript
// before: 0 is falsy, so "start at 0" silently becomes "start at latest"
const cursor = startIndex ? String(startIndex - 1) : undefined;
// after: an explicit start always wins, and 0 means "no cursor, from the beginning"
const hasStart = startIndex !== undefined;
const cursor = hasStart && startIndex > 0 ? String(startIndex - 1) : undefined;
const from = hasStart ? undefined : options.from;

02 · Run it on your appThe check

Do any of your API options treat 0 or an empty string as “not provided”?

If the honest answer is no or not sure, this is worth a look before it reaches production.

Want this checked on your code?

Tell us what you are building and what worries you. We match a senior reviewer to your code and confirm scope before anything starts.

— Get in touch
hi@invental.co ↗
— Or
— What to include

A link or short description of the code or system, what you want checked, your stack, and when you need the answer. No repository access is needed until scope is agreed.

— Or leave a note
We reply within one business day.