Test against bogus or impossible user input

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the user-input-and-output category.

Last Updated: 2021-05-16

I had the following code for finding advisor entities within a certain distance of the postcode the user gave us:

<? php
    $geocodeResults = Geocoder::geocodeAddress($postcode);

    // We do not filter on the language provided in the
    // reverseAuctionRequest OR private/business tax types. This is because
    // most advisors were created by admin and do not have language
    // information available
    return Advisor::subscribed()->orderByDistance(
        $geocodeResults["latitude"], $geocodeResults["longitude"]

What I had not anticipated, but what actually happened, was users gave us postcodes that do not exist. In this case, geocodeResults["latitude"] would end up as null, which breaks the orderByDistance method, since it expects numerical lat/long arguments.

To fix this, it would have needed to fail gracefully if this was null.


Whenever their is user input - and especially if fed into another system (e.g. geocoding), test what happens if the input is bogus (e.g. a fake address) – and handle it gracefully.