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
What is the main problem with a variable named `data` holding a list of active user records?
In this round
What is the main problem with a variable named `data` holding a list of active user records?
A catch block re-throws with `throw new Error('Failed to save user ' + user.id, { cause: err })`. What is the purpose of preserving `err` as `cause`?
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 function `calculateTotal(cart)` is extracted out of a larger `checkout()` function so it can be unit tested independently. What is the primary clean-code benefit of this extraction?
A teammate needs a comment to explain what a function named `chk(u, r)` does. What is the clean-code fix?
Which function name is the clean-code choice for a function that returns milliseconds elapsed for a given number of days?
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`
Compare these two versions of a discount check:
A) `if (user) { if (user.isActive) { if (user.orders.length > 10) { return 0.2 } else { return 0.1 } } else { return 0 } } else { return 0 }`
B) `if (!user || !user.isActive) return 0
if (user.orders.length > 10) return 0.2
return 0.1`
Which is cleaner, and why?
What does the "Boy Scout Rule" mean in the context of clean code?
What is the best test for whether a function is doing "one thing," per clean-code guidance?
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?