Do you use null condition operators when getting values from objects

Updated by Tiago Araújo [SSW] 3 years ago. See history

123

Null-conditional operators - makes checking for null as easy as inserting a single question mark. The Null-conditional operators feature boils down all of the previously laborious clunky code into a single question mark.

int length = customer != null && customer.name != null ? customer.name.length : 0;

❌ Figure: Figure: Bad example - Verbose and complex code checking for nulls

int length = customers?.name?.length ?? 0;

✅ Figure: Figure: Good example - Robust and easier to read code

acknowledgements
related rules