11

So.. I'm giving one of my employers webapps a visual refresher, new company branding and whatnot.

And then I stumbled onto a check that is not returning what anybody expects, and, well , I'm busy fixing things, yeah..? so I go digging.. 🤔

```
function isDefined(obj) {
return !(typeof obj === "undefined") || obj !== null;
}
```

Here's the fun part, these particular lines have been in the code base since before 2017, which is when my Git history starts, because that's when we migrated projects from Visual SourceSafe 6 over to Git. Yes, you read that right. They were still using VSS in 2017.

I've begged and pleaded with my last 3 bosses to let us thrown this piece of shit out our second story window and rewrite it properly. But no, we don't have time to rewrite, so we must fix what we have instead.

I lost 4 hours of my life earlier today, tracking down another error that has been silently swallowed by a handler with its "console.log" call commented out, only to find that it's always been like that, and it's an "expected error". 🤦

Please, just fucking kill me now... I just, I can't deal with this shit anymore.

Comments
  • 0
    What's wrong with the code? I am not JS literate.
  • 1
    Should be && instead of ||
    Also it could've been written as just `return obj != null;`
  • 1
    @hardCoding well, first keep in mind that in JS `null` and `undefined` are different things.. the former is defined with no value, while the latter is not defined.

    The way this check was done, along with the way it was being used, means that cases where you would expect the check to return `false` were actually returning `true` instead. Which led to needing further checks on whatever value was being checked in the places it was implemented.

    The logic in this particular project doesn't really care about the difference between `null` and `undefined`, we treat both as "falsey".. which is just a really loose definition of "false".

    JS is a beautifully fucked up monstrosity.. lol.
  • 0
    Thanks, I was confused because to me or (||) seems to be appropriate here.
  • 2
    @Akhetopnu yeah, I came to the same conclusion.. ended up with `return obj !== undefined && obj !== null`, which allowed me to simplify almost 20 of the places where this was called.

    The problem now is that, due to how pedantic I am about code clarity, I want to rename the function.. because technically it doesn't only check if something "is defined", it actually checks if something "is initialised"... 🤦

    Alas, I have bigger fish to fry today. So I'll make some notes (read: tech debt) and come back to it another day. 🖖
Add Comment