Skip to content

Files

Latest commit

 

History

History
28 lines (20 loc) · 795 Bytes

prefer_is_empty.md

File metadata and controls

28 lines (20 loc) · 795 Bytes

Pattern: Missing use of isEmpty/isNotEmpty

Issue: -

Description

The Iterable contract does not require that a collection know its length or be able to provide it in constant time. Calling length just to see if the collection contains anything can be slow.

Instead, there are faster and more readable getters: isEmpty and isNotEmpty. Use the one that doesn't require you to negate the result.

Example of correct code:

if (lunchBox.isEmpty) return 'so hungry...';
if (words.isNotEmpty) return words.join(' ');

Example of incorrect code:

if (lunchBox.length == 0) return 'so hungry...';
if (words.length != 0) return words.join(' ');

Further Reading