Skip to content

_.isDefined (Duplicate/Resurrected Issue) #2652

@machineghost

Description

@machineghost

First off, I'm aware that this was originally closed in #137. I'm resurrecting it because I felt neither that issue nor it's multiple duplicates gives a strong justification of why _.isDefined adds value to the library (so I figured I'd give it a shot).

Basically what it all boils down to is your 5th grade English class: double-negatives ("nobody can not ignore") are prohibited in English. Why? They make your brain work harder, for no good reason: "everyone can ignore" means the same thing as "nobody can not ignore", but the latter requires more brain cycles.

What does that have to do with code? Well, programmers' as a group would almost universally agree that:

if (!(foo === 'bar')) // who does this?

if (foo !== 'bar') // everyone does this

Now, consider that isUndefined is very often used as follows:

if (!_.isUndefined(foo)) {
    doSomething(foo);
}

Myself, the people who filed the duplicates, the people who thumbs-upped the original ticket (that's a lot of people who took time to support something so trivial!) ... we all feel that the above code is like !(foo === 'bar'), and we wish to use the foo !== 'bar' equivalent:

if (_.isDefined(foo)) { ...

Now, to be fair, it's not a massive cognitive load to mentally translate the former in the latter. But when given:

if (!_.isUndefined(foo)) { .. // vs.
if (_.isDefined(foo)) { ...

I think the vast majority of programmers would agree the latter is easier read/process.

Lodash is an entire library made up of small conveniences which make developers' lives easier and their code cleaner and easier to understand. It seems to me that _.isDefined fits that criteria perfectly.

P.S. To our BDFL JDalton's point:

I'm not a fan of _.isUndefined to begin with

I would argue that it's all about 0 (although sometimes also about other "falsy" values too). When you have a function which can possibly take a 0, but which wants to invalidate other "falsy" values (a fairly common occurrence, at least in my experience), there's no better/cleaner way to do so than _.isUndefined:

function(foo) {
   if (!foo && foo !=== 0) // lame
   if (_.isUndefined(foo)) // awesome (especially if foo has a longer name)

Or, if other "falsy" values are allowed:

   if (!foo && foo !=== 0 && foo !=== false && foo !== '') // really lame
   if (_.isUndefined(foo)) // even more awesome

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions