molindo / eslint-config-molindo

ESLint config that implements the Molindo styleguide and helps to catch errors.
MIT License
1 stars 2 forks source link

Explicit length checks #13

Closed amannn closed 3 years ago

amannn commented 5 years ago

Code like this can be confusing:

const isValidEnterSubmit = isEnter && searchResults.length;

If there are results and you console.log(isValidEnterSubmit) you might see a number like 12 as the last value in the condition is assigned to the variable. Explicit length checks like searchResults.length > 0 avoid this issue and is therefore preferred.

Also in JSX this is problematic:

<div>{searchResults.length && searchResults[0].title}</div>

This will render 0 if there are no results, as numbers are emitted to the DOM, but booleans are not.

Theres a rule for this in an external plugin, but I'd like to avoid adding yet another plugin – at least as long as consumers of this package have to install plugins separately.