admin管理员组

文章数量:1289583

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?

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?

Share Improve this question edited Mar 13, 2020 at 15:49 Uwe Keim 40.8k61 gold badges188 silver badges303 bronze badges asked Aug 1, 2019 at 15:09 gaborgabor 1,7293 gold badges13 silver badges14 bronze badges 5
  • 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 Commented Aug 1, 2019 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 Commented Aug 1, 2019 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 Commented Aug 1, 2019 at 15:14
  • The actual subject is covered here stackoverflow./a/50211024/295783 – mplungjan Commented Aug 1, 2019 at 15:15
  • 1 Not sure how this question can have an answer since it is really personal opinion. – epascarello Commented Aug 1, 2019 at 15:20
Add a ment  | 

2 Answers 2

Reset to default 13

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

So: removeNullish or similar.

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 mon to see in functional programming, namely the Maybe monad which es with a subtype to represent nothing: Nothing or None.

If you look at monetjs:

The Maybe type is the most mon 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 ramda.js, such function is called isNil:

Checks if the input value is null or undefined

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

本文标签: javascriptIs there a word that covers both Null and UndefinedStack Overflow