Leeds Postcode Bug Traced to indexOf Returning Zero in a Truthiness Check
A same-day delivery check built on four postcode areas worked for most regions but failed for Leeds, though it had launched alongside Cardiff and Truro. The cause was using indexOf, which returns a position rather than a yes-or-no answer, so a match at index zero evaluates as false in an if statement. Other matches returned positive numbers and passed, which is why the defect went unnoticed in code review and testing. The article notes that Python's str.find has the same pitfall, with -1 signalling absence. It recommends using includes in JavaScript or comparing indexOf against -1, and testing boundary cases such as the first item, the last item, and a missing value.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in