Is there a more succinct way of doing this in Kotl...
# announcements
m
Is there a more succinct way of doing this in Kotlin?
s
where are you getting
minDistance
from?
m
Stored earlier within the same function
Untitled.kt
I tried ☝️ but I need to perform two operations, so the ?: was not working for me
s
it seems like maybe you should consolidate assignment of
minDistance
m
Untitled
I need minDistance available to two different functions
s
two different functions? is it supposed to be a class member instead?
m
It’s declared in a parent function Two functions within the parent scope use minDistance
s
a parent function? isn’t this function a private class member?
I’m having trouble following the intent of the function (there’s a lot of context and it’d be hard to convey over the internet), but I’d recommend not overly relying on
.let
if you can
m
Okay, I might go back to using if( != null )
s
you can also avoid unnecessary nesting of scopes by returning early in the function and maybe
continue
- ing early in the loop
m
It’s more legible
s
unsure how the types shake out here but you might just be able do this early on?
Copy code
val dataSet = lineData.getDataSetByLabel("BG", true) ?: return
b
Boolean truth tables to the rescue:
Copy code
if (minDistance == null || (minDistance != null && calcDistFromBolus < minDistance)) {
  minDistance = calcDistFromBolus
  closestEntry = bolusStartEndEntries[0].data as BolusDisplayModel
}
👍 2
m
Thanks @Bob Glamm … I got so wrapped up in trying to use scoping functions
Sometimes the basics are better
Thanks again @Shawn
👍 1
j
wouldn't be better to use something like
Copy code
Int.MIN_VALUE
instead of null ?
👍 1
s
not in the case where any integer value is potentially valid
it would also make
calcDistFromBolus < minDistance
always false
(well,
min_value
anyhow, you could use max instead maybe)
j
yeah I mean for your context of usage of course... I think it's better to avoid null values in cases like that... and if you say "any integer" is potentially valid for min/max, you are already kinda in trouble with overflow
m
@Jiri Bruchanov That worked… but I’m using Int.MAX_VALUE
Didn’t know that existed
j
👍
m
Originally I was using: minDistance = 999999 But I felt like that looked dirty
r
Untitled
⬆️ More or less the same as @Bob Glamm, but I think that we don’t need the null check