xenoterracide
06/12/2019, 5:59 PMval 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 { ... }
Luke
06/12/2019, 6:04 PM?: run { log.error("monitor {} not found", dto.monitorSn ) }
?Ruckus
06/12/2019, 6:10 PMval monitor = useCase.findMonitor(dto.monitorSn)
if (monitor == null) {
log.error("monitor {} not found", dto.monitorSn)
} else {
useCase.connect(transform(dto))
}
Mike
06/12/2019, 6:27 PMrun
. 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.xenoterracide
06/12/2019, 6:59 PMMike
06/12/2019, 10:31 PMRuckus
06/12/2019, 10:49 PMif
for whatever reason, I'd probably do something more like:
useCase.findMonitor(dto.monitorSn) ?: run {
log.error("monitor {} not found", dto.monitorSn)
return
}
useCase.connect(transform(dto))
Ruckus
06/12/2019, 10:53 PMMike
06/12/2019, 11:39 PMreturn 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.gildor
06/13/2019, 1:45 AMbut it may be more readable to people less kotlin-fluent.It more readable for everyone
Alowaniak
06/13/2019, 9:01 AMmonitor
isn't even used? Why no hasMonitor
or something and do an if or whenilya.gorbunov
06/13/2019, 6:44 PMlog.error
instead of null
monitor, when it is nullMike
06/13/2019, 8:12 PMxenoterracide
06/13/2019, 8:26 PMopen 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).