9

I sometimes have to write code in JavaScript that handles both null and undefined the same way. For example, a function that removes from an array every item that is either null or undefined. How should I name the function?

removeNullAndUndefined is very long. removeNull is short but imprecise. Ideally I want to name it removeX, where X has the meaning "null or undefined".

Does anyone know a good word for X?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
gabor
  • 1,561
  • 2
  • 12
  • 13
  • 1
    Depends on context. "Empty" is the general term, but if it can be a string, "empty" may also mean "a string with no characters in it", which may or may not be desirable. – Dave Newton Aug 01 '19 at 15:11
  • So you are asking a good name for a function that would remove null and undefined value without using the term removeNullAndUndefined (I am totally fine with this by the way). I would go with : removeFalsyValues. but at this point there isn't a "good word" you have to made it up so it's kind of up to you I guess – MaieonBrix Aug 01 '19 at 15:13
  • @MaieonBrix - But `0`, `""`, `NaN`, and `false` are also falsy, and the OP seems to want to leave them alone. There is a word for this: "nullish." See my answer. – T.J. Crowder Aug 01 '19 at 15:14
  • The actual subject is covered here https://stackoverflow.com/a/50211024/295783 – mplungjan Aug 01 '19 at 15:15
  • 1
    Not sure how this question can have an answer since it is really personal opinion. – epascarello Aug 01 '19 at 15:20

2 Answers2

11

Yes: Nullish. For instance, in the current Stage 3 Nullish Coalescing Operator proposal.

So: removeNullish or similar.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I'd prefer "nully", like "truthy". (Its opposite, of course, is antNully. Go tell antNully.) – Dave Newton Aug 01 '19 at 15:12
  • @DaveNewton - Regardless of one's preferences, "nullish" has been around for a couple of years now (at least). :-) – T.J. Crowder Aug 01 '19 at 15:15
  • Unlike @DaveNewton, I prefer a,b,c,d for my variables and make sure I use i and l together so you really have to concentrate on what my code does. – epascarello Aug 01 '19 at 15:19
2

When I need to check for null or undefined I often name such function either isNil or isNothing e.g.

const isNil = thing => thing === null || thing === undefined;

If you must adopt a naming convention I'd opt for what's quite common to see in functional programming, namely the Maybe monad which comes with a subtype to represent nothing: Nothing or None.

If you look at :

The Maybe type is the most common way of representing nothingness (or the null type) with making the possibilities of NullPointer issues disappear.

Maybe is effectively abstract and has two concrete subtypes: Some (also Just) and None (also Nothing).

Under the hood, it uses a isNothing function for checking for null or undefined

In , such function is called isNil:

Checks if the input value is null or undefined

(Lodash has a similar method with the exact same name.)

Community
  • 1
  • 1
customcommander
  • 17,580
  • 5
  • 58
  • 84