dimanche 17 novembre 2019

What is the standard for choosing "when" vs "if/else" in Kotlin?

I'm making the transfer from Java to Kotlin, and learning about the varied differences between the Java switch and the Kotlin when statement.

My question is, do the differences change the standard for when you choose "when" over a series of "if" and "else if" blocks?

For example, in Java "switch" is generally more efficient than "else-ifs" when there are 5 or more cases, and roughly comparable otherwise. If-else is generally preferred when the statements are boolean. Is this similar in Kotlin? Are there other reasons that differ to Java that I should consider when writing my code - ie standards for readability etc.

I would appreciate answers or links to more in depth reading on the subject - I have struggled to find any sources that do a good cost-benefit analysis.

Aucun commentaire:

Enregistrer un commentaire