Nat Strangerweather
07/27/2022, 12:16 PMval skillRating = MutableLiveData(0)
fun calculateSkillRating() {
if (readWinCount.value != 0 && readLossCount.value != 0) {
skillRating.value = averageWinningScore.value?.plus(
(400 * (readWinCount.value?.minus(readLossCount.value!!)!!) /
(readWinCount.value?.plus(readLossCount.value!!)!!))
)
}
}
Jurriaan Mous
07/27/2022, 12:49 PM?:
operator can be useful to set some values to 0 if they are null
val skillRating = MutableLiveData(0)
fun calculateSkillRating() {
val readWinCountValue = readWinCount.value ?: 0
val readLossCountValue = readLossCount.value ?: 0
val averageWinningScoreValue = averageWinningScore.value ?: 0
if (readWinCountValue != 0 && readLossCountValue != 0) {
skillRating.value = averageWinningScoreValue + 400 * (readWinCountValue - readLossCountValue) /
(readWinCountValue + readLossCount)
}
}
Nat Strangerweather
07/27/2022, 12:52 PMStephan Schroeder
07/27/2022, 12:54 PMfun calculateSkillRating() {
if (readWinCount.value != 0 && readLossCount.value != 0) {
val avarageWinningScoreValue = checkNotNull(averageWinningScore.value)
val readWinCountValue = checkNotNull(readWinCount.value)
val readLossCountValue = checkNotNull(readLossCount.value)
skillRating.value = averageWinningScoreValue + (400 * (readWinCountValue -readLossCountValue) /
(readWinCountValue + readLossCountValue)
)
}
}
Stephan Schroeder
07/27/2022, 12:57 PMnull
while my code throws an IllegalStateException
, the original code threw a NullPointerException
if one of the properties was null
which I assume wasn't handled anywhere, so technically my code is closer to the original implementation.
You have to decide if using 0 instead is ok in the context of the code.Nat Strangerweather
07/27/2022, 1:02 PMNullPointerException
with this in the main code:
val skillRating by statsViewModel.skillRating.observeAsState(initial = 0)
But yes, 0 is the value I would want to start with.davec
07/27/2022, 3:13 PM!!
in Kotlin, that's basically telling the compiler you know better than it does, and you're introducing the possibility of a NullPointerException
. That's Java thinking! Instead, you should check if something is null before using it using any one of a number of mechanisms that Kotlin provides (if
, when
, elvis operator, let
, etc.)davec
07/27/2022, 3:15 PMNat Strangerweather
07/27/2022, 3:22 PM!!
in my code and often get nullpointers. I think I need to read up on nullables. ๐
davec
07/27/2022, 3:25 PMcalculateSkillRating
should have input parameters and return a value rather than having side effects:
fun calculateSkillRating(readWinCountValue: Int, readLossCountValue: Int, averageWinningScore: Int): Double =
averageWinningScore + (400 * (readWinCountValue-readLossCountValue) /
(readWinCountValue + readLossCountValue))
(The code may not be exactly correct, I'm not sure of the types, but you get this idea)
Here the purpose of the function is clear, easy to unit test, and gets rid of all the noisy error cases. You might also have a special case for when the win/loss count are zero, but I'd argue that none of those input parameters should be nullable, and that the function doesn't mutate state skillRating.value = ...
but instead just does a simple calculation and you do the assignment elsewhere.
Disclaimer though: I don't have your full source code, and this is sort of just a sketch of what I'd recommend.