Code review lab
Find the bug. Choose the test. Defend the repair.
Zero is a choice
Missing or null input defaults to 3. Zero disables reminders. Inputs in this lab are already valid.
function reminderCount(value) {
return value || 3;
}What blocks approval?
Answer
Zero is falsy. The draft overwrites a valid user choice.
Which test exposes it?
reminders = 2
- Draft returns
2- Repair returns
2
reminders = 0
- Draft returns
3- Repair returns
0
reminders = null
- Draft returns
3- Repair returns
3
Answer
The draft returns 3. The repair returns the required 0.
Which repair preserves the contract?
Answer
Nullish defaulting preserves zero. Validation is already outside this small lab function.
Review decision
Request nullish defaulting and a regression test for zero.
function reminderCount(value) {
return value ?? 3;
}If the input boundary also accepts strings or negative numbers, defaulting alone is not validation. Ask for that contract.
Full caseThe hidden side effect
Return the earliest shifts. Preserve the caller’s array order. The comparator only reads start times.
function earliest(shifts) {
return shifts.sort((a, b) => a.start - b.start);
}What blocks approval?
Answer
The draft mutates the input even when its returned order is correct.
Which test exposes it?
Already ordered: A, B
- Draft returns
[ { "id": "A", "start": 10 }, { "id": "B", "start": 20 } ]- Repair returns
[ { "id": "A", "start": 10 }, { "id": "B", "start": 20 } ]- Input after draft
A, B- Input after repair
A, B
Caller order: B, A
- Draft returns
[ { "id": "A", "start": 10 }, { "id": "B", "start": 20 } ]- Repair returns
[ { "id": "A", "start": 10 }, { "id": "B", "start": 20 } ]- Input after draft
A, B- Input after repair
B, A
Empty array
- Draft returns
[]- Repair returns
[]- Input after draft
- Input after repair
Answer
Both return A, B, but only the draft also changes its input to A, B.
Which repair preserves the contract?
Answer
Only array order changes here. Copying the array preserves the caller’s order.
Review decision
Sort a copy. Test the return value and the input after the call.
function earliest(shifts) {
return [...shifts].sort((a, b) => a.start - b.start);
}A shallow copy protects array order. It does not protect nested objects if a later implementation edits their fields.
Full caseThe exact cutoff
An event stays active only while its age is strictly less than 60 seconds. Ages in this lab are nonnegative seconds.
function isActive(age) {
return age <= 60;
}What blocks approval?
Answer
The draft includes the endpoint. The contract excludes it.
Which test exposes it?
Age = 59 seconds
- Draft returns
true- Repair returns
true
Age = 60 seconds
- Draft returns
true- Repair returns
false
Age = 61 seconds
- Draft returns
false- Repair returns
false
Answer
The draft returns true, but the contract requires false.
Which repair preserves the contract?
Answer
A strict comparison matches the stated interval.
Review decision
Use a strict comparison. Test below, at, and above the cutoff.
function isActive(age) {
return age < 60;
}If a product owner wants the endpoint included, the expected behavior changes. The comparator follows the contract.
Full case