


On the other hand, it feels like calling takeIf() or takeUnless() on an expression should at least be a warning in IntelliJ. The takeIf() and takeUnless() functions aren’t doing anything you can’t do with if/else, it just makes things easier to read in some cases (which I value highly when writing code). I like the fact if is an expression in Kotlin, and I think that reduces some of the utility of takeIf() and takeUnless(). I do find places in my code to use it, but more often than not I use an if/else expression instead. Closing ThoughtsĪs we’ve seen, the places where we might use takeIf() and takeUnless() can be fairly subjective. takeIf Īgain, it’s subjective that the takeIf() version is any better, but some might find easier to read.
