What do I do?

  • Software engineer at Toast
  • Mostly Android
  • Originally Java, increasingly using Kotlin

3 things I like about Kotlin

  • 1) Handling nulls
  • 2) Functional collection methods
  • 3) Extension functions

Some features I used when converting from Java

1) Null views in Android

view?.displayInvalidCardErrorMessage()
if (view != null) {
    view.hideProgressSpinner();
}

Kotlin:

Java:

  • Cases in Android where we must check the view is not null 
  • Kotlin makes this
    • more succinct + readable
    • forces null checks when type is nullable

2) Functional collections methods

  • Refactored from Java -> Kotlin
  • FluentIterable 
  • Kotlin collections methods

Functional collections methods 

fun Check.getFirstOpenCreditCardPayment(): Optional<Payment> {
    return Optional.fromNullable(payments.firstOrNull { payment ->
            payment.paymentType == Payment.Type.CREDIT &&
                    !payment.isDeleted &&
                    payment.paymentStatus == Status.OPEN })
}
private Predicate<Payment> isOpenCreditCard = payment -> {	
    return payment.paymentType == Payment.Type.CREDIT 
        && !payment.isDeleted() 
        && payment.paymentStatus == Status.OPEN;	
};

private Optional<Payment> filterPayments(Check check, Predicate condition) {	
    Optional<ToastPosOrderPayment> result = FluentIterable.from(check.payments)	
                .filter(condition)	
                .first();	
    return result;	
}
    
public Optional<Payment> getFirstOpenCreditCardPayment(Check check){	
    return filterPayments(check, isOpenCreditCard);	
}

FluentIterable: 

Kotlin built in collections methods:

3) Extension Functions

  • Write new functions for a class without altering or extending it
fun CardReaderService.canTakeEmvPayment(): Boolean {
    return (canTakeQuickChipPayment() || canTakeFullEmvPayment())
} 

deck

By Nasreen Hunaina

deck

  • 103