Test your Clean Code knowledge with a free interactive quiz — 28 questions with answers and explanations. No signup needed to play.
Question 1/12Score 0
Why is a boolean "flag parameter" (e.g. `save(user, true)`) considered a code smell?
In this round
Why is a boolean "flag parameter" (e.g. `save(user, true)`) considered a code smell?
Two versions of an error handler:
A) `try { db.users.save(user) } catch (e) { console.log('error') }`
B) `try { return db.users.save(user) } catch (err) { throw new Error(`Failed to save user ${user.id}: ${err.message}`, { cause: err }) }`
Which is the clean-code choice?
A teammate needs a comment to explain what a function named `chk(u, r)` does. What is the clean-code fix?
Which version of a magic-number check is the clean-code choice?
A) `if (user.orders.length > 10) applyDiscount(0.2)`
B) `const VIP_ORDER_THRESHOLD = 10
const VIP_DISCOUNT_RATE = 0.2
if (user.orders.length > VIP_ORDER_THRESHOLD) applyDiscount(VIP_DISCOUNT_RATE)`
Which comment adds real value under clean-code guidance?
A) `// increment i by 1` above `i++`
B) `// Stripe webhooks can arrive out of order during retries, so we ignore events older than the one we already processed` above an `if (event.createdAt < lastProcessedEvent.createdAt) return`
A `checkout(cart, user)` function validates the cart, computes tax, saves the order, and sends a confirmation email -- all in one function body. What clean-code principle does this violate?
What is the best test for whether a function is doing "one thing," per clean-code guidance?
What is the main problem with a variable named `data` holding a list of active user records?
What is wrong with encoding a variable's type into its name, e.g. `usersArray` or `nameStr`?
A block of code has a comment reading `// check if user can edit (admin or owner)` directly above a function named `chk(u, r)`. What is the clean-code critique?
What is the risk of a function name like `processAndValidateAndSave(order)`?
Compare two ways of writing input validation for `title` and `body` fields, duplicated identically inside both `createPost(data)` and `updatePost(data)`, versus extracting it into a shared `validatePost(data)` called by both. Which is the clean-code choice and why?