Code review lab

Find the bug. Choose the test. Defend the repair.

01

Zero is a choice

Missing or null input defaults to 3. Zero disables reminders. Inputs in this lab are already valid.

Draft
function reminderCount(value) {
			return value || 3;
		}

What blocks approval?

What blocks approval?
Answer

Zero is falsy. The draft overwrites a valid user choice.

Which test exposes it?

Which test exposes it?
Answer

The draft returns 3. The repair returns the required 0.

Which repair preserves the contract?

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.

Small repair
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 case
02

The hidden side effect

Return the earliest shifts. Preserve the caller’s array order. The comparator only reads start times.

Draft
function earliest(shifts) {
			return shifts.sort((a, b) => a.start - b.start);
		}

What blocks approval?

What blocks approval?
Answer

The draft mutates the input even when its returned order is correct.

Which test exposes it?

Which test exposes it?
Answer

Both return A, B, but only the draft also changes its input to A, B.

Which repair preserves the contract?

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.

Small repair
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 case
03

The exact cutoff

An event stays active only while its age is strictly less than 60 seconds. Ages in this lab are nonnegative seconds.

Draft
function isActive(age) {
			return age <= 60;
		}

What blocks approval?

What blocks approval?
Answer

The draft includes the endpoint. The contract excludes it.

Which test exposes it?

Which test exposes it?
Answer

The draft returns true, but the contract requires false.

Which repair preserves the contract?

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.

Small repair
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