Dependency misuse
A well-named API that does not do what its name says.
What it looks like
sort()with no comparator, which sorts numbers as strings.replacewith a string pattern, which changes the first match only.parseIntwithout a radix, orJSON.parseon something that may not be JSON.
Why it survives review
The call reads correctly in English, and for a large fraction of inputs it behaves correctly too — [1, 2, 3].sort() is right, and so is replacing a substring that appears once. The name is doing the reviewing, and the name is not lying so much as being read charitably.
How to see it
- When a call looks obviously right, that is the moment to check the signature. The ones that survive review are the ones that read well.
- Know the specific traps in your standard library.
sortwithout a comparator,replacewithoutg,==aroundnull. - Ask what the function does for the inputs you have *not* pictured: an empty one, a duplicate, a negative, a multi-byte character.
A minimal pair
Correct
return values.slice().sort((a, b) => a - b);
Defective
return values.slice().sort();
[10, 9] sorts to [10, 9], because the default comparator compares '10' and '9' as strings.
Practise it
1 diff in the corpus carry this class. They are not listed, because knowing which diff contains what would make finding it a comprehension question about this page.
Go to the exercises