Youssef Shoaib [MOD]
06/05/2025, 7:09 PMSam
06/05/2025, 7:35 PMPablichjenkov
06/05/2025, 10:34 PMPablichjenkov
06/05/2025, 10:35 PMYoussef Shoaib [MOD]
06/05/2025, 11:11 PMPablichjenkov
06/05/2025, 11:15 PMefemoney
06/05/2025, 11:15 PMT
gives you something optional, ie its either there or its not there.
Using Optional<T>
the model is not any better than just using null ie T?
.efemoney
06/05/2025, 11:21 PMgildor
06/06/2025, 3:21 AMphldavies
06/06/2025, 9:00 AM{"myField": null}
and {}
gildor
06/06/2025, 9:27 AMPablichjenkov
06/06/2025, 1:52 PMnull
is not needed in a language. I don't recall last time I have declared a variable null-able
.
You can always replace null
with a specific initial value.
var type: Type? = null
vs
var type: Type = TypeInitialValue
gildor
06/06/2025, 1:57 PMPablichjenkov
06/06/2025, 1:58 PMwhen {
nullable > 0 -> computeStuffPos()
nullable < 0 -> computeStuffNeg()
0 -> computeZero()
null -> error()// non intuitive what to do
}
Vs
val InitialValue = 0
when {
nonnullable > 0 -> computeStuffPos()
nonnullable < 0 -> computeStuffNeg()
InitialValue -> computeZero()
}
You see how null introduces entropy in the top when
gildor
06/06/2025, 2:00 PMPablichjenkov
06/06/2025, 2:03 PMgildor
06/06/2025, 2:36 PMPablichjenkov
06/06/2025, 2:55 PMAlso null is better than 0 or -1 or "" in most of the cases as initial value
This one is the key here. Right, when it comes to primitive types yes. But in cases of data classes, model classes and richer types. I would prefer to define my own initial value as a constant or object to express the non initialized state. But for primitives I agree with yougildor
06/09/2025, 1:39 AM