Open question: what code pattern you use usually for null safety problem

mw mingwu at gmail.com
Thu Jan 14 19:49:06 UTC 2021


On Thursday, 14 January 2021 at 18:24:44 UTC, ddcovery wrote:
> I know there is other threads about null safety and the 
> "possible" ways to support this in D and so on.
>
> This is only an open question to know what code patterns you 
> usually use to solve this situation in D:
>
>   if(person.father.father.name == "Peter") doSomething();
>   if(person.father.age > 80 ) doSomething();
>
> knowing that *person*, or its *father* property can be null
>
> i.e.: the incremental null check solution

I just use this most simple one:

> if(
>   person !is null &&
>   person.father !is null &&
>   person.father.father !is null &&
>   person.father.father.name == "Peter"
> )
> {
>   doSomething();
> }

Reason: easy to read and reason about, esp for non-authors of 
this piece of the code.



More information about the Digitalmars-d-learn mailing list