-
Notifications
You must be signed in to change notification settings - Fork 14
Using nullable Booleans #18
Comments
I strongly prefer 2 because it is clear that you are dealing with a nullable boolean. In contrast, 3 can be easily confused with a typical beginners mistake of |
@matklad I think that the inversion of the boolean that you have to do in option 2 (the statement is executed when the value is |
Can we have a bright highlighting in IDE for |
I don't think that's actually necessary, because the refactored code will not compile. We do need to highlight |
I think it is somewhat a frustrating experience to attempt to refactor |
@yole hm, agree. Then it is the question of educating people about the fact that And just a terrible idea, can a special syntax be invented for nullable booleans, like |
We've discussed the possibility of handling nullable booleans in |
Referencing from Kotlin/kotlin-style-guide#18, I prefer this variant. Signed-off-by: Harsh Shandilya <[email protected]>
* treewide: Get rid of all double bangs Replace with proper nullity handling * Fix databinding errors * Change code style around nullable booleans Referencing from Kotlin/kotlin-style-guide#18, I prefer this variant.
I think version 2 is actually quite clear, because the literal |
When you have a variable
val value: Boolean?
you have a couple of possible ways to test it fortrue
:if (value != null && value)
(doesn't work with mutable properties)if (value ?: false)
if (value == true)
I prefer version 3 because it makes the intent very clear.
In many cases you have an additional possibility: Write an extension function that works on nullable types. Example:
nullableString.isNullOrEmpty()
The text was updated successfully, but these errors were encountered: