DEV Community

Discussion on: 1 line of code: How to check if an Array is empty

Collapse
 
hnicolas profile image
Nicolas Hervé

A "universal library" should respect language best practices. There is a standard way to deal with errors in javascript and you just introduce a new solution that is really questionable in my opinion.

You say throwing an error would force the use of a try catch block but your solution introduce the need to check the result too.
One big problem is that Error is a truthy value so that is error prone for the caller of the function :

if(isEmptyArray({})) {
  // oups its not an empty array
}
Enter fullscreen mode Exit fullscreen mode

It is a bad practice to have a function that can return different types in js (boolean | Error) and having the caller to check the return type. If you really need to, you could use a tuple or an object that the caller can deconstruct.

Some comments have been hidden by the post's author - find out more