Filtering Values One of the more basic transformations that we can perform on a collection is to ensure that all of the elements meet certain criteria. Maybe this sort of thing doesn’t happen too often, but it’s something I’ve started noticing more and more as I read code in the wild.ĭid I miss a case where takeIf() and takeUnless() would be appropriate, or a case when they introduce errors? Let me know, I’m always happy to chat and appreciate feedback. The Kotlin standard library offers a lot of functions for performing operations on collections. The Kotlin standard library gives us a number of built-in ways to achieve this so that we can better focus on our code. 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |