Skip to content

Taxonomy

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.
  • replace with a string pattern, which changes the first match only.
  • parseInt without a radix, or JSON.parse on 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

  1. 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.
  2. Know the specific traps in your standard library. sort without a comparator, replace without g, == around null.
  3. 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