Developer tools
Edge cases
Inputs at the edges of what's allowed: where code goes wrong most often.
Why it exists
A programmer keeps the usual case in mind while writing code, and that case works. An empty cart, a zero discount, an order of exactly 3000, a one-letter name are easy not to imagine. Users will imagine them for you: someone always submits an empty form. Edge cases are a way to ask "what if…" systematically instead of waiting for a bug report.
Where it's used
- Rules with thresholds: "free shipping from 3000", "passwords of 8+ characters", "a discount for orders over 10 items": test exactly the threshold and its neighbors.
- Lists and strings: an empty array, one item, an empty string, a string of spaces. Everyone gets empty carts and empty searches.
- Money and numbers: zero, negative amounts (refunds), fractions of a cent, very large sums.
- User input: null and undefined instead of a value, stray spaces, text instead of a number.
Syntax
// Edge case checklist
// empty: [], "", {}, null, undefined
// zero and one: 0, [x], "a"
// sign: -1, a negative amount
// boundary: exactly the limit, limit - 1, limit + 1
// huge: 1e21, a very long string, a big arrayExamples
function average(numbers) {
let sum = 0;
for (const n of numbers) sum += n;
return sum / numbers.length;
}
average([2, 4]); // → 3
average([]); // → NaN — the edge case nobody thought ofconst freeShipping = (total) => total > 3000;
freeShipping(5000); // → true
freeShipping(500); // → false
freeShipping(3000); // → false — the rule says "from 3000", so it should be true"".split(","); // → [""] — not an empty array!
"a,b".split(","); // → ["a", "b"]
Math.max(...[]); // → -Infinity
String(1e21); // → "1e+21"Results after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
Bugs gather at the edges for clear reasons. Comparisons: > and >= differ at exactly one point, the boundary, and only a test of that point tells them apart. Emptiness: dividing by the length of an empty array gives NaN, Math.max() of an empty list gives -Infinity, and "".split(",") gives an array with one empty string. Sign: code written for positive numbers quietly does nonsense with negative ones. Size: huge numbers lose precision and print as 1e+21.
Hence boundary analysis: for every rule, take the value on the boundary and one on each side. And equivalence classes: out of all the "ordinary" values, one is enough. 500 and 600 test the same thing; 0 and 3000 test different things.
One more rule: one edge test, one oddity. If a password is both short and digit-free, the test can't tell which rule kicked in.
Traps
When to pick something else
Don't test edges that can't happen: if a function is only called by your own code with already-checked data, a null test in every spot is noise. Edge cases matter at the boundaries of the system: user input, server responses, a module's public functions.