Time: 15 minutes. Output: a ranking and three lines.
The task: return a user's overdue invoices, sorted by amount, largest first. Read each implementation for thirty seconds. Rank them, with one line per implementation naming the thing that decided it.
A
async function getData(uid) {
const res = await db.query('SELECT * FROM invoices WHERE user_id = $1', [uid]);
let out = [];
for (let i = 0; i < res.rows.length; i++) {
if (res.rows[i].status !== 'paid') {
if (new Date(res.rows[i].due_date) < new Date()) {
out.push(res.rows[i]);
}
}
}
out.sort((a, b) => b.amount - a.amount);
for (const o of out) { o.label = `${o.client_name} — $${o.amount}`; }
return out;
}
B
class InvoiceQueryBuilder {
constructor(repo) { this.repo = repo; this.filters = []; this.sorter = null; }
where(fn) { this.filters.push(fn); return this; }
orderBy(fn) { this.sorter = fn; return this; }
async execute(userId) {
let rows = await this.repo.findByUser(userId);
for (const f of this.filters) rows = rows.filter(f);
return this.sorter ? rows.sort(this.sorter) : rows;
}
}
const overdue = (userId) =>
new InvoiceQueryBuilder(invoiceRepo)
.where(i => i.status !== 'paid')
.where(i => i.dueDate < new Date())
.orderBy((a, b) => b.amount - a.amount)
.execute(userId);
C
async function overdueInvoices(userId, today = new Date()) {
const { rows } = await db.query(
`SELECT id, client_name, amount, due_date
FROM invoices
WHERE user_id = $1 AND status <> 'paid' AND due_date < $2
ORDER BY amount DESC`,
[userId, today]
);
return rows;
}
Model answer — Rep 5.1
Ranking: C, A, B.
C. The name says the thing. One job. The filter and the sort happen in the query, which is what the database is for, and today is a parameter, so the function can be tested on a fixed date. Nine lines.
A. It works. The name says nothing, the filter is a nested loop in application code, and the last line formats a label, which is a second job hiding in a query function. new Date() is created on every iteration, which is harmless today and a tell about how the code was written.
B. An abstraction with one caller. It reads the whole table into memory and filters there, and it adds a layer every reader must trace through to find out that nothing happens in it. This is what over-generation looks like: it is the most complete answer to a question nobody asked.
If you ranked B above A because it looks more professional, that is the recognition error from the list: abstraction for quality. It is the most common one in code, and the one agents trigger most.