The following doesn’t look right to me somehow. I think return null
means to return from foo
, not from when
, but my gut doesn’t trust it:
fun foo(owner: User, startEvent: Event?, endEvent: Event?): Message {
val m = Message(owner,
when {
endEvent != null -> MsgType.END_DATE
startEvent != null -> MsgType.START_DATE
// RETURN FROM foo()? OR FROM when?
else -> return null
})
m.emailWorthy = true
save(m)
// This looks totally normal
return m
}
The when
statement really looks like a list of functions that take a Boolean
and return a MsgType
. Thinking code-as-data, I think that’s how the compiler sees it, so that’s a good thing. I’d say that I don’t like mixing data flow with evaluation, but with a throw
, the code-as-data model works for understanding the when
statement. It’s only when I see return
there, it makes me uneasy somehow.
I feel that when reading the code, the return is hidden in a place my eye doesn’t want to look for it. Thoughts?
If this is a “bad” code smell, how should I rewrite the above?