Use if
when you have exactly one truthy and one falsy case and you don't need an implicit do
block. In contrast, when
should be used when you only have to handle the truthy case and the implicit do
. There is no difference in speed, it's a matter of using the most idiomatic style.
(if (my-predicate? my-data)
(do-something my-data)
(do-something-else my-data))
(when (my-predicate? my-data)
(do-something my-data)
(do-something-additionally my-data))
In the if
case, only do-something
will be run if my-predicate?
returns a truthy result, whereas in the when
case, both do-something
and do-something-additionally
are executed.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…