Prompt
When dealing with possibly missing or nullable values (especially globals and config), make null/undefined handling explicit and never call a value unless you’ve verified its type.
Apply this standard:
- Use explicit checks for undefined:
x === undefined(orx !== undefined) rather than relying on implicit truthiness. - Guard optional callbacks: before calling
maybeFn(...), ensuretypeof maybeFn === 'function'. - If refactoring boolean logic around undefined/missing values, add/adjust unit tests to confirm behavior is unchanged.
Example:
function isDocumentVisible(document) {
return (
document.visibilityState === undefined ||
document.visibilityState === 'visible' ||
document.visibilityState === 'prerender'
);
}
function shouldRetry(query, error) {
const retry = query.config.retry;
if (retry === true) return true;
if (typeof retry === 'number') return query.state.failureCount <= retry;
if (typeof retry === 'function') {
return retry(query.state.failureCount, error);
}
return false;
}