Do you use null condition operators when getting values from objects

Loading last updated info...

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

Categories

Authors

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star