Timeline for Optimal way to use null conditional operators in boolean expressions
Current License: CC BY-SA 3.0
4 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Nov 22, 2024 at 1:55 | comment | added | Zeus |
@Santhos, 4 is not thread-safe (Manager can potentially be modified between the checks) so should be avoided whenever possible. 3 is objectively better (except that the first ? should be removed as you already accessed team unconditionally).
|
|
| Mar 13, 2018 at 1:10 | comment | added | ToolmakerSteve |
@Santhos -- re "the programmer's intention seems a bit clearer". IMHO, this is a case where the first time a programmer sees a?.b == true they will be confused, but once they grasp what it is doing, it becomes a very readable idiom, much nicer than having to test != null in the middle of a complex expression. Same for a?.b ?? false, which seems to have gained traction as the most popular solution, because it matches what you would type if you were dealing with a type other than boolean [though I don't like it for boolean; for me, it doesn't read as naturally; I prefer == true].
|
|
| Mar 29, 2016 at 4:57 | comment | added | Santhos | I think this would definitely be the way to go if we wanted to use the null conditional operator, because it is also linq safe. The question is whether the readability is good. From my point of view, the choice is between opt 3 and opt 4 and I would choose 4 over 3 for the readability reason, also the programmer's intention seems a bit clearer. I have marked Ben Cottrell's answer as correct because I like that approach a bit more. If I could mark two answers, I would. | |
| Mar 27, 2016 at 18:21 | history | answered | svick | CC BY-SA 3.0 |