Share
Embed
Effective Kotlin
A lightning talk about how Kotlin helps you to write effective code based on Effective Java by Joshua Block.
Marcello Galhardo
April 17, 2019
Transcript
39 slides
-
// Kotlin object Singleton // Java public class Singleton {
private Singleton INSTANCE; private Singleton() { } public Singleton getInstance() { if (INSTANCE != null) { INSTANCE = new Singleton(); } return INSTANCE; } } -
public class Api { public List<Object> doSearch( String deviceToken //
other parameters ) { String customerId = getCustomerIdFromSessionToken(deviceToken); // Do something and returns the search return ArrayList(); } private String getCustomerIdFromSessionToken(String authToken) { // Retrieve the customer through the user session return "CustomerId"; } } -
public class Api { public List<Object> doSearch( String deviceToken //
other parameters ) { String customerId = getCustomerIdFromSessionToken(deviceToken); // Do something and returns the search return ArrayList(); } private String getCustomerIdFromSessionToken(String authToken) { // Retrieve the customer through the user session return "CustomerId"; } } -
public class Api { public List<Object> doSearch( String deviceToken //
other parameters ) { String customerId = getCustomerIdFromSessionToken(deviceToken); // Do something and returns the search return ArrayList(); } private String getCustomerIdFromSessionToken(String authToken) { // Retrieve the customer through the user session return "CustomerId"; } } -
public class Api { public List<Object> doSearch( String deviceToken //
other parameters ) { String customerId = getCustomerIdFromSessionToken(deviceToken); // Do something and returns the search return ArrayList(); } private String getCustomerIdFromSessionToken(String authToken) { // Retrieve the customer through the user session return "CustomerId"; } } -
public class Api { public List<Object> doSearch( String deviceToken //
other parameters ) { String customerId = getCustomerIdFromSessionToken(deviceToken); // Do something and returns the search return ArrayList(); } private String getCustomerIdFromSessionToken(String authToken) { // Retrieve the customer through the user session return "CustomerId"; } } -
class Api { fun doSearch( deviceToken: String // other parameters
): List<Any> { val customerId = getCustomerIdFromSessionToken(deviceToken) // Do something and returns the search return ArrayList() } private fun getCustomerIdFromSessionToken(authToken: String): String { // Retrieve the customer through the user session return "CustomerId" } } -
class Api { fun doSearch( deviceToken: String // other parameters
): List<Any> { val customerId = getCustomerIdFromSessionToken(deviceToken) // Do something and returns the search return ArrayList() } private fun getCustomerIdFromSessionToken(authToken: String): String { // Retrieve the customer through the user session return "CustomerId" } } -
class Api { fun doSearch( sessionToken: String // other parameters
): List<Any> { val customerId = getCustomerIdFromSessionToken(sessionToken) // Do something and returns the search return ArrayList() } private fun getCustomerIdFromSessionToken(sessionToken: String): String { // Retrieve the customer through the user session return "CustomerId" } } -
class Api { fun doSearch( sessionToken: SessionToken // other parameters
): List<Any> { val customerId = getCustomerIdFromSessionToken(sessionToken) // Do something and returns the search return ArrayList() } private fun getCustomerIdFromSessionToken(sessionToken: SessionToken): String { // Retrieve the customer through the user session return "CustomerId" } } -
// Kotlin class Foo open class Foo // Java public
final class Foo {} public class Foo {} -
public interface Collection<E> : Iterable<E> { public val size: Int
public fun isEmpty(): Boolean public fun contains(element: E): Boolean public fun containsAll(elements: Collection<E>): Boolean } -
sealed class ViewState { object Started: ViewState() object EditMode: ViewState()
object ViewMode: ViewState() } fun verifyState(state: ViewState) { when (state) { is ViewState.Started -> println("Started") is ViewState.EditMode -> println("EditMode") is ViewState.ViewMode -> println("ViewMode") } } -
sealed class ViewState { object Started: ViewState() object EditMode: ViewState()
object ViewMode: ViewState() class Error(val message: String = "Unknown error"): ViewState() } fun verifyState(state: ViewState) { when (state) { is ViewState.Started -> println("Started") is ViewState.EditMode -> println("EditMode") is ViewState.ViewMode -> println("ViewMode") } } -
sealed class ViewState { object Started: ViewState() object EditMode: ViewState()
object ViewMode: ViewState() class Error(val message: String = "Unknown error"): ViewState() } fun verifyState(state: ViewState) { when (state) { is ViewState.Started -> println("Started") is ViewState.EditMode -> println("EditMode") is ViewState.ViewMode -> println("ViewMode") } } when expressions guarantee that their branches exhaustively check the possible subclasses of a sealed class. -
'when' expression on sealed classes is recommended to be exhaustive,
add 'Error' branch or 'else' branch instead -
sealed class ViewState { object Started: ViewState() object EditMode: ViewState()
object ViewMode: ViewState() class Error(val message: String = "Unknown error"): ViewState() } fun verifyState(state: ViewState) { when (state) { is ViewState.Started -> println("Started") is ViewState.EditMode -> println("EditMode") is ViewState.ViewMode -> println(“ViewMode") is ViewState.Error -> println(state.message) } } -
fun requireNotNull(any: Any?) { if (any == null) throw IllegalArgumentException()
} fun foo(s: String?) { requireNotNull(s) s.length } -
Only safe (?.) or non-null asserted (!!.) calls are allowed
on a nullable receiver of type String? -
fun requireNotNull(any: Any?) { contract { returns() implies (any !=
null) } if (any == null) throw IllegalArgumentException() } fun foo(s: String?) { requireNotNull(s) s.length }