---
title: Use isEmpty instead of comparing length to zero
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > Use isEmpty instead of comparing length to zero
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Use isEmpty instead of comparing length to zero

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

**ID:** `dart-code-quality/prefer-is-empty`

**Language:** Dart

**Severity:** Notice

**Category:** Code Style

## Description{% #description %}

Comparing a collection's .length to zero (e.g. list.length == 0) is less readable and may be slower than calling .isEmpty or .isNotEmpty directly. Use .isEmpty when checking for zero length and .isNotEmpty when checking for non-zero length.

## Non-Compliant Code Examples{% #non-compliant-code-examples %}

```dart
void main() {
  var list = [];
  if (list.length == 0) {}
  if (list.length != 0) {}
  if (list.length > 0) {}
  if (0 == list.length) {}
  if (0 != list.length) {}
}
```

## Compliant Code Examples{% #compliant-code-examples %}

```dart
void main() {
  var list = [];
  if (list.isEmpty) {}
  if (list.isNotEmpty) {}
  if (list.length == 1) {}
  if (list.length > 1) {}
}
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 