Coding basics: clean data first before operating on data

When you're coding a routine that operates on input, validate and clean the data early on and try to return soon as possible.  There's no reason to add new variables to the stack, or call any other functions if the input isn't clean.  If you were processing a birth date, you wouldn't need to continue if the month was < 1 or > 12.  You would return from that function early before going further.

The example function below takes a Google API key, address string, and URL to the API.  If successful, it returns an object with latitude and longitude returned from Google Maps.

Note how the data is confirmed to be OK early on.  Through each step, the data is tested and if something fails, a NULL value is returned.  When execution hits the end, the Google API call is good and we have our latitude and longitude.

The calling routine will expect the geo object with latitude and longitude, or NULL if failed.