question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pick a default `non-zero` option for `explicit-length-check`

See original GitHub issue

https://github.com/sindresorhus/eslint-plugin-unicorn/blob/f1ced468120686263a45a9993d2f02343f70c6f3/docs/rules/explicit-length-check.md#non-zero-comparisons

I suggest picking a default value for the non-zero option.

not-equal

Pros:

  1. !== 0 is the inverse of === 0, like “is it not empty?” and “is it empty?”
  2. !== 0 does not perform type coercion (considering that the general consensus is that implicit type coercion is bad)

Cons:

  1. If array.length is undefined, array.length !== 0 would evaluate to true, which is incorrect if we are asking “is it not empty?”

greater-than

Pros:

  1. If array.length is undefined, array.length > 0 evaluates to false, which is “more correct” than the !== 0 case
  2. (subjective) A positive question in a condition is more readable than a negative question, like is it populated? for > 0 vs is it not empty? for !== 0

Cons:

  1. Performs type coercion (this actually might be good in this case, see pro # 1)

greater-than-or-equal

Pros:

  1. same as for greater-than
  2. same as for greater-than

Cons:

  1. same as for greater-than
  2. (unconfirmed) More mentally demanding because it actually has two conditions in one expression (greater than 1 OR equal to 1)
  3. (unconfirmed) Used less often than !== 0 or > 0

I didn’t perform a thorough analysis when I started using !== 0 in my code, and now I think that actually > 0 is a better option.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
sindresorhuscommented, Apr 29, 2020

I agree. I prefer greater-than as default.

1reaction
EvgenyOrekhovcommented, Apr 29, 2020

@ryaxc Your example is not polymorphic. There are other types than Array that could have length.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set default values for fields or controls - Microsoft Support
This article explains how to set a default value for a table field or for a control on a form in an Access...
Read more >
How to store a specific non-zero default value in a linked field?
Solved it. What one has to do is to make changes in schema. Then, go to Tools --> Update Database Structure --> go...
Read more >
How to set Default value for Numeric Control on Front Panel?
I have several input values that are really configuration parameters that I want to set with non-zero Default values.
Read more >
NonZeroUsize in std::num - Rust
For example, Option<NonZeroUsize> is the same size as usize : ... The default implementation is almost always sufficient, and should not be overridden ......
Read more >
Float property non-zero default, is it possible? - Stack Overflow
Note that you still have to set the property to the default in TMyObj's ... it by setting the value I want as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found