``` val monitor = useCase.findMonitor(dto.moni...
# announcements
x
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
What about
?: run { log.error("monitor {} not found", dto.monitorSn ) }
?
r
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
@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
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
If that's all there is, then I'd be inclined to use expression body and '?:' for the log statement.
r
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
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
but it may be more readable to people less kotlin-fluent.
It more readable for everyone
a
The
monitor
isn't even used? Why no
hasMonitor
or something and do an if or when
i
@Mike I don't think it was intended to return the result of
log.error
instead of
null
monitor, when it is null
m
@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
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).