https://kotlinlang.org logo
#announcements
Title
# announcements
x

xenoterracide

06/12/2019, 5:59 PM
Copy code
val monitor = useCase.findMonitor(dto.monitorSn)?.also {
            useCase.connect(transform(dto))
        }
        if ( monitor == null ) {
            log.error("monitor {} not found", dto.monitorSn )
        }
is there a nicer way to do the monitor == null as sort of an
orElse { ... }
l

Luke

06/12/2019, 6:04 PM
What about
?: run { log.error("monitor {} not found", dto.monitorSn ) }
?
r

Ruckus

06/12/2019, 6:10 PM
I think you're making it way too hard to read. What's wrong with
Copy code
val monitor = useCase.findMonitor(dto.monitorSn)
if (monitor == null) {
    log.error("monitor {} not found", dto.monitorSn)
} else {
    useCase.connect(transform(dto))
}
✔️ 3
👍 2
m

Mike

06/12/2019, 6:27 PM
@Luke As it only does one thing, you don’t need the
run
. Can just do the
?: log
, can’t you? @Ruckus I’m on the fence for your comment. Comes down to familiarity, and exact context of code around it. But I do agree that sometimes too much ‘ingenuity’ is put into avoiding a very readable
if != null
check.
👍 1
x

xenoterracide

06/12/2019, 6:59 PM
yeah, in this case there isn’t any more code around it, this is basically the entirety of the body of the method. I don’t know, an if statement makes it feel… more procedural, and in this case rather unnecessary, but it may be more readable to people less kotlin-fluent.
m

Mike

06/12/2019, 10:31 PM
If that's all there is, then I'd be inclined to use expression body and '?:' for the log statement.
r

Ruckus

06/12/2019, 10:49 PM
If that's the whole function, and you really don't like the
if
for whatever reason, I'd probably do something more like:
Copy code
useCase.findMonitor(dto.monitorSn) ?: run {
    log.error("monitor {} not found", dto.monitorSn)
    return
}
useCase.connect(transform(dto))
Trying to cram multiple separate ideas into single expressions just because you can is very much an anti-pattern.
m

Mike

06/12/2019, 11:39 PM
I guess it comes down to how used to FP you are with map, flatMap, fold and mapLeft. For me,
Copy code
return useCase.findMonitor(dto.monitorSn)?.also {
    useCase.connect(transform(dto))
} ?: log.error("monitor {} not found", dto.monitorSn )
Reads like an FP chain. Bottom line, let the team norms drive what you do, as that’s the most important thing. Generally, it doesn’t matter what we think, but what your teammate’s think and expect to see in this situation.
g

gildor

06/13/2019, 1:45 AM
but it may be more readable to people less kotlin-fluent.
It more readable for everyone
a

Alowaniak

06/13/2019, 9:01 AM
The
monitor
isn't even used? Why no
hasMonitor
or something and do an if or when
i

ilya.gorbunov

06/13/2019, 6:44 PM
@Mike I don't think it was intended to return the result of
log.error
instead of
null
monitor, when it is null
m

Mike

06/13/2019, 8:12 PM
@ilya.gorbunov I suspect you’re right. I mis-read the code, or there’s insufficient code for either of us to be quite sure what the rest of it does…
x

xenoterracide

06/13/2019, 8:26 PM
Copy code
open fun connect(dto: FoleyDto) {
        log.trace("saving: {}", dto)
        useCase.findMonitor(dto.monitorSn)?.also {
            val foley = transform(dto)
            useCase.connect(FoleyConnectionEvent(it, foley, gateway)) { patient, dest ->
                template.convertAndSend("monitor.$dest.patient", patient)
            }
        } ?: log.error("monitor {} not found, unable to connect foley", dto.monitorSn)
    }
that’s the whole thing, working on testing the template call now. I guess I see it as wanting to execute the left or right side of a xor, rather than so much as avoid if ( == null).
2 Views