easyWrong PatternsJavaScript
The double negative
A developer asked an AI assistant to add a check that skips sending a marketing email to users who have opted out. The suggested condition compiles and passes a quick test, but a bug report shows some opted-out users are still receiving emails after an unrelated change nearby.
marketing.js
function shouldSendMarketingEmail(user) {
if (!user.preferences.notMarketingOptOut) {
return false;
}
return true;
}A teammate later 'simplifies' this by removing the if/return and just returning the condition directly. What's the real risk with this function as written?
JavaScript evaluates the ! operator twice on boolean values, canceling out the negation silently.
notMarketingOptOut is a double negative (true means 'not opted out') which is easy to misread as 'opted out' and invert by accident during a future edit.
The function will throw if user.preferences is ever an empty object.
return false inside an if block is not valid syntax in strict mode.
Sign in with GitHub to submit an answer and track your progress.