SSW Foursquare

Do you use null condition operators when getting values from objects

Last updated by Tiago Araújo [SSW] almost 2 years ago.See history

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: Bad example - Verbose and complex code checking for nulls

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

Figure: Good example - Robust and easier to read code

We open source. Powered by GitHub