The differences between true, false, and null (or how i accidentally made every single cat hate children)

rockykev

Rocky Kev

Posted on October 28, 2019

The differences between true, false, and null (or how i accidentally made every single cat hate children)

The Problem

I was contracted to update the PetFinder V2 API for a Pet Adoption Agency. It's a simple project - a handful of headaches here and there. But nothing that I couldn't figure out.

If you're curious on how to pull API data via PetFinder V2, let me know in the comments. Happy to share.

The payload gives you things like:

  • Animal Name
  • Animal Type
  • Animal Species

It also gives you things like:

  • If the animal is housetrained
  • If the animal has been declawed.
  • If the animal can co-exist with cats/dogs/children (<-- my OOOF!)

My display

My original newbie solution

The payload gives you the following:

"attributes": {
                "spayed_neutered": false,
                "house_trained": true,
                "declawed": false,
                "special_needs": null,
                "shots_current": false
            },
Enter fullscreen mode Exit fullscreen mode

Ah, neat!

The piece that I want to point out is this.

    "declawed": false,
Enter fullscreen mode Exit fullscreen mode

To read this --
If true, the animal "has been declawed".
If false, the animal "has NOT been declawed".

Easy - right?

So that means, this code works either way.

if ($declawed) {
   $string = "Has been declawed (image of a mitten). ";
} else {
   $string = "Has not been declawed (image of a claws)."
}

Enter fullscreen mode Exit fullscreen mode

If the payload says true, then kitty doesn't have any sharp bits. (btw, please don't declaw.)

If the payload says false, or NULL, then kitty has what nature gave it.

Simple stuff. Coding 101.

AH CRAP - I'm an idiot

I rolled it out, did my little celebration dance to FF1 Victory Theme while wearing my 'I'm a real developer' hat, and called it a day.

Then I get a email a few days later with the following:

It appears that the information we are entering into PetFinder to appear on our website is wrong. For example, Piper the dog shows she is NOT good with cats on our website.

AH CRAP.

This wasn't a Petfinder issue. This was a Rocky issue.

FALSE != NULL

So the problem came with my True/False Statement.

            "environment": {
                "children": null,
                "dogs": false,
                "cats": true
            },
Enter fullscreen mode Exit fullscreen mode

Let's break this down.

True == Good with [x]. (So the pet is good with children)
False == NOT Good with [x]. (So the pet is NOT good with dogs)
Null == no data. (The user did not submit this info.)

Based on my code & logic, poor Stripey the cat would have been given a 'NOT GOOD WITH CHILDREN' label. Which is NOT TRUE!

"children": null
Enter fullscreen mode Exit fullscreen mode

The admins who were submitting data to PetFinder would leave off a lot of data, creating a lot of NULLs. Which is very common, btw, so plan for that.

The payload says that value doesn't exist.

But my stupid code assumed all Nulls meant false. Which is completely inaccurate. And I totally messed up.

Updated Code

Really, I need to identify 3 situations.

If the variable is null.
THEN if the variable is true, or the variable is false.

Let's look at my new code.

For those not familiar with PHP, isset checks to see if the variable has a value.

Note: True/false are values. Null is NOT!

    if (!isset($good_with_children)) {
    $string =  "this data doesn't exist.";
    } else {

        if ($good_with_children == false) {
      $string = 'Is NOT good with Children. (sad face)';
    } elseif ($good_with_children == true) {
      $string = 'Is good with Children. (happy face)';
    }
    }
Enter fullscreen mode Exit fullscreen mode

What does this mean?

I fixed the issue by adding another conditional in there to check to see if it's NULL or not.

Check if $good_with_children is NULL. If it's NULL, then spit the 'data doesn't exist' bit. (Which you'll remove in production!)

If it isn't NULL, go do the true/false test.

Conclusion

Huzzah! This conditional fixed the issue. Every single cat on that adoption site no longer hates children. (Only a few do.)

Well, you live and learn.

And I hope my code helps these pets get into loving homes.

💖 💪 🙅 🚩
rockykev
Rocky Kev

Posted on October 28, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related