Why Avoid the Lodash isEmpty function?

Note Statistics

Note Statistics

  • Viewed 265 times

Generally it is best to avoid the Lodash isEmpty function. We should try to avoid it for the reason below

  1. By design is supposed to work with enumerable variables (Object, Array, Sets, Map, Buffer etc).
  2. There is no type check when you pass other data types. This means that passing the wrong value would not throw an error. But you might not get what you expect for booleans and numeric values.

Some examples copied from the documentation:

.isEmpty(null);
// => true, I expected this. 

.isEmpty(true);
// => true. You expected this? I did not. 

.isEmpty(1);
// => true. You expected this? I did not.

.isEmpty([1, 2, 3]);
// => false, I expected this.

.isEmpty({ 'a': 1 });
// => false, I expected this.

.isEmpty('');
// => true, I expected this.

.isEmpty(NaN);
// => true, I expected this.

.isEmpty(undefined);
// => true, I expected this.

isEmpty is a too generic name - What does is empty mean?

Regardless of the programming language, a function named isEmpty is ambiguous. If it is a class method we get a context. But when it is a function there is a problem. It does not say anything about the value it is checking.

So what it an empty value? The word empty used in the context of enumerable makes sense. But when looking at other data types it becomes confusing. For example

  • What is an empty boolean? Is false considered an empty value?
  • What is an empty number? Is there such a thing?

Reminds of truthy | falsy check: This function remind of thruthy (!!) check which is also misleading. For example an empty array or object would still be thruthy.

!![]
// => true

!!{}
// true

Similar to isEmpty of lodash using truthy check should be avoided as it leaves room for errors.

What we can use in instead?

There are standardand basic JavaScript checks that are more explicit.

  • String: Use String.length
  • Array: Use Array.length
  • Buffer: Use Buffer.length
  • Object: Use Object.keys(targetObject).length
  • Set: Use Set.size
  • Map: Use Map.size
  • Number Use value > 0 or isNaN
  • Boolean: Use value === true or value === false

Yes this is more expressive but the intent of the code is clear. Also remember that you using this is telling someone else to use it. They probably might not understand that your use case is different.

Authored by
faboulaws