Refactoring to Kotlin Codelab
Written by Nikos Vaggalis   
Tuesday, 23 July 2019

Learn how to convert Java to Kotlin using Kotlin's idioms with Google Developers Codelabs. Refactoring to Kotlin, which is available in English, Brazilian and Chinese, provides a guided, hands-on coding experience in a running time of just less than an hour.

 codelabsbanner

While the latest IDE's do a good job in automatically converting Java code into Kotlin, this doesn't necessarily help you understand the mapping from the one code base to the other. Moreover, given you are naturally predisposed to continue writing Kotlin as if it was Java, following Java's practices and idioms, this won't make the most of Kotlin's fresh approach. The Refactoring to Kotlin Codelab aims to change that.

The lab begins where the automatic conversion of two simple Java classes, User and Repository, to their Kotlin counterparts ends, cherry picking the converted code to not just improve it but also teach Kotlin in the process.

In that process you'll learn about :

  • Handling nullability
  • Implementing singletons
  • Data classes
  • Handling strings
  • Elvis operator
  • Destructuring
  • Properties and backing properties
  • Default arguments and named parameters
  • Working with collections
  • Extension functions
  • Top-level functions and parameters
  • let, apply, with, and run keywords

It requires firing up Android Studio or IntelliJ IDEA, copy the two sample classes and run the converter to turn them from .java to .kt.

Conversion pitfalls

The converter while doing its job properly, still needs a bit of help to become more efficient.

For example, User class has two properties: firstName and lastName which can be null, something that the converter did not pick up.So there's two options here, either annotate your Java class properties with org.jetbrains.annotations.NotNull or androidx.annotation.NonNull and re-run the conversion or directly modify the Kotlin code by suffixing "String" with a "?" which Kotlin uses to denote nullability.

The rest of the tutorial enriched the code with more improvements such as marking the User class as a "data" Kotlin class, using default and named arguments, using string templates, ending up with a run down on the let, apply, etc functions.

In summary, this targeted quick mini course cleverly teaches the basics of Kotlin by leveraging the existing knowledge in Java but shifting the paradigm to Kotlin - it's much easier to learn by observing how the old constructs map to their newest counterparts than starting from scratch.

While this scratches the surface, there's much more to learn. If you need complete schooling over Kotlin, I suggest enrolling in the (free) Kotlin Bootcamp for Programmers, or the Android-targeted Developing Android Apps with Kotlin, both by Udacity. Or if you prefer to learn from books I can recommend The Programmer's Guide to Kotlin and Android Programming in Kotlin: Starting With An App from the I Programmer Library, many extracts from which are available on this site.

 

More Information

Refactoring to Kotlin

Related Articles

Udacity's Developing Android Apps with Kotlin 

The Programmers Guide To Kotlin - Inline Functions

Android Programming In Kotlin: Events

<ASIN:1871962536>

<ASIN:1871962544>

 

 

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 

Banner


All Quiet In RedMonk's Language Rankings
19/05/2023

The latest iteration of the bi-annual RedMonk Language Rankings took place in January 2023, but the results have only just been publicized. One possible reason for the delay could be there was so litt [ ... ]



Demis Hassabis & Fei-Fei Li Are Up Beat On AI
17/05/2023

With so many doomsayers around at the moment it is almost a refreshing breeze to hear people talk about the positive side of AI. In a video Demis Hassabis and Fei-Fei Li talk about AI and Science.


More News

 

picobook

 



 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Tuesday, 23 July 2019 )