Do you use null condition operators when getting values from objects

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

123
<introEmbed body={<> 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

Liam Elliott
Matt Wicks
Related rules

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