Timeline for Optimal way to use null conditional operators in boolean expressions
Current License: CC BY-SA 3.0
20 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Apr 13, 2018 at 18:42 | comment | added | Brain2000 | VB.net used to be so behind but these days it's wonderful. Mixing nullable/non-nullable in a shortcut Boolean works fine. I was converting it to C# for a project when I ran across the same problem. If Team.Category = "A Team" AndAlso team?.Manager?.IsVietnamVet Then.... | |
| Oct 2, 2017 at 11:27 | comment | added | Santhos |
I started to use nullableBool == true if I don't create a wrapper. It is readable because you normally don't write == true when using regular boolean as @Flater mentions, so it suggests that the variable is nullable. Aditionally, it improves LINQ readability because you don't use multiple null conditions as @Fabio mentions.
|
|
| Oct 1, 2017 at 4:50 | comment | added | Fabio | Using multiple null conditional operators will far away from readable code. | |
| Sep 29, 2017 at 12:54 | comment | added | Flater |
"Would you really write if (condition == true) without any hesitation?" For a normal boolean, no, since it's superfluous. However, for a nullable boolean, this is relevant. nullableBool == true is basically testing for nullableBool != false && nullableBool != null (and it's this second part that makes it useful and therefore not superfluous)
|
|
| Sep 29, 2017 at 11:07 | history | tweeted | twitter.com/StackSoftEng/status/913722007119974402 | ||
| Sep 29, 2017 at 7:31 | answer | added | Ant P | timeline score: 3 | |
| Mar 29, 2016 at 11:40 | comment | added | svick |
Looking at the code more carefully, the null conditional part should be team.Manager?.IsVietnamVet, i.e. no null conditional after team, since already can't be null.
|
|
| Mar 29, 2016 at 4:49 | vote | accept | Santhos | ||
| Mar 29, 2016 at 4:48 | history | edited | Santhos | CC BY-SA 3.0 |
added 4 characters in body
|
| Mar 27, 2016 at 18:41 | answer | added | Ben Cottrell | timeline score: 5 | |
| Mar 27, 2016 at 18:21 | answer | added | svick | timeline score: 6 | |
| Mar 26, 2016 at 16:36 | comment | added | Santhos | @AdamZuckerman Why? It is just an example, you could have a bool property there instead. Why do you care about the data when the question is clearly about the syntax. | |
| Mar 25, 2016 at 2:56 | answer | added | Logerfo | timeline score: -4 | |
| Mar 24, 2016 at 23:29 | comment | added | Adam Zuckerman | The boolean comparison with a string makes no sense. What is "A Team" supposed to represent? | |
| Mar 24, 2016 at 23:27 | comment | added | Santhos | @StevieV it was ment to be like this right from the start ;) | |
| Mar 24, 2016 at 23:17 | comment | added | Snoop | Ok well category is right? EDIT: Now I've confused myself... because the question was edited? | |
| Mar 24, 2016 at 23:16 | comment | added | Santhos | @StevieV Manager property is not a string, sorry if that was not clear before | |
| Mar 24, 2016 at 23:15 | history | edited | Santhos | CC BY-SA 3.0 |
added 226 characters in body
|
| Mar 24, 2016 at 23:10 | comment | added | Snoop | if(!(String.IsNullOrEmpty(team.Manager) && condition)) ... | |
| Mar 24, 2016 at 22:56 | history | asked | Santhos | CC BY-SA 3.0 |