Using Kotlin takeIf (or takeUnless)

In Kotlin’s standard functions, there’s two function i.e. takeIf and takeUnless, that at first glance, what’s so special about it? Is it pretty much just if?

Or one could go the extreme, replace every if it sees as below (NOT recommended).

// Original Code
if (status) { doThis() }
// Modified Code
takeIf { status }?.apply { doThis() }

--

--

--

Sharing iOS, Android and relevant Mobile App Development Technology and Learning

Recommended from Medium

Dây curoa Alpha Power Belt 3-B134

The top 10 time-saving Airtable date formulas

Experimenting GitHub Container Registry

Unity: Exploring Multiplayer Solutions

Easy way to do AWS Glue Job creation using AWS CLI

Gripping Java8 Date & Time API

Augmenting the Revolution of A Digital Era

How Can I Quickly Integrate Cloud Storage of AppGallery Connect into My Cocos-based App?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Elye

Elye

Sharing Software Development Experience, focus on Mobile. https://elye-project.medium.com/subscribe https://twitter.com/elye_project

More from Medium

Speed Up Mandelbrot Drawing on Android Jetpack Compose

What is Android Jetpack Compose?

Getting Started with ConstraintLayout in Jetpack Compose

Jetpack Compose for Views developers — LinearLayout