Back to all reviewers

prefer nullish coalescing operator

langgenius/dify
Based on 2 comments
TSX

Use the nullish coalescing operator (`??`) instead of logical OR (`||`) when you want to provide default values only for null or undefined, while preserving other falsy values like empty arrays, empty strings, or zero.

Null Handling TSX

Reviewer Prompt

Use the nullish coalescing operator (??) instead of logical OR (||) when you want to provide default values only for null or undefined, while preserving other falsy values like empty arrays, empty strings, or zero.

The || operator treats all falsy values (null, undefined, false, 0, “”, []) as requiring the default, while ?? only treats null and undefined as requiring the default. This distinction is crucial for preserving intentionally falsy but valid values.

Example:

// ❌ Problematic - loses empty arrays
...(draft.file?.allowed_file_types || [])

// ✅ Better - preserves empty arrays  
...(draft.file?.allowed_file_types ?? [])

// ❌ Problematic - loses falsy but valid values
const config = userConfig || defaultConfig

// ✅ Better - only defaults for null/undefined
const config = userConfig ?? defaultConfig

This pattern is especially important when working with arrays, objects, or other values where falsy states have semantic meaning that should be preserved.

2
Comments Analyzed
TSX
Primary Language
Null Handling
Category

Source Discussions