Auto Added by WPeMatico

From Java to Kotlin in 20 minutes ⚡️

What is the experience like as a Java developer to start programming in Kotlin?

Credits: Katerine Salazar Gonzalez

What is the experience like as a Java developer to start programming in Kotlin?

I didn’t remember, it was years ago for me!

Fortunately, a mob-programming session with my colleagues gave me the chance to see again things with a beginner’s mind.

Show me the code!

To follow along, check out the code. You need to have IntelliJ Community Edition installed. It’s free! The code lives in the java and kotlin branches at https://github.com/jmfayard/from-java-to-kotlin. You can see all the changes described below in this pull-request

From Java to Kotlin by jmfayard · Pull Request #1 · jmfayard/from-java-to-kotlin

Let’s start by explaining the context of our story.

Mob-programming

My colleagues Sarah and Peter and I were doing a session of Mob programming.

The goal was to solve the kata of The observed PIN, where an unreliable spy tells that he saw the PIN 1357 being used. But actually, he’s not quite sure. Each digit could be one of its neighbors on the keyboard layout. So, the actual PIN could be 1357, but also for example 2357 or 1368.

The project was a Java project built with Maven. It contains two files: PinGuesser.java and PinGuesserTest.java. It compiles and runs the unit tests in a matter of seconds, not minutes, like in many Android apps. That makes for a better developer experience IMHO.

We were using IntelliJ’s Code With Me to write the code together.

We were doing well and have solved the Kata in Java, then refactored it to a satisfactory state.

  • Sarah: Is there anything else we could improve?
  • Peter: I don’t know, looks good to me.
  • Me: Well, we have 20 minutes left, why not rewriting the whole thing in Kotlin?
  • Sarah: Oh, I’ve heard about Kotlin but haven’t had the chance to use it yet. 20 minutes though, do you think we can do it?
  • Me: Let’s get started and see where it leads us!

Tools > Kotlin > Configure Kotlin in project

  • Peter: Ok, so I have never done any Kotlin in my life, tell me what to do.
  • Me: There is a command IntelliJ called Convert Java File to Kotlin File. It’s a great starting point!
  • Peter: Let’s give it a try.
  • Peter: IntelliJ tells me that Kotlin is not configured, that makes sense. How do I configure Kotlin in Maven?
  • Me: I don’t know, I’ve always used Gradle. Just let IntelliJ do it! By the way, what it will do is the same thing as Tools > Kotlin > Configure Kotlin in project.
  • Peter: Let’s do it…. It seems to have worked. There are updates to the file pom.xml.
  • Peter: first commit.

Tell Java that @ParametersAreNonnullByDefault

  • Me: Before we try the Java to Kotlin converter, there is something we want to take care of. As you know, Kotlin has integrated nullability in the type system while Java by default has not. Therefore, the converter is going to allow nulls everywhere, which is technically correct but not what you want.
  • Sarah: But there are annotations in Java to say if something is nullable or not, right?
  • Me: Exactly! And the one we want is to tell by default everything is non-null. Conveniently, it’s exactly how it works in Kotlin too.

https://medium.com/media/99627ac0574358fa44ac4a593276f40b/href

PinGuesser: Convert Java File to Kotlin File

  • Peter: I guess I now open PinGuesser.java and just relaunch the converter Convert Java File to Kotlin File .
  • Me: Correct.
  • Peter: It seems that… it worked? There is a file PinGuesser.kt.
  • Me: How do you know it worked, though?
  • Sarah: You should run the unit tests.
  • Peter: Right.
  • Peter: It’s still all green. Amazing, I have written my first Kotlin code ever, and it is bug-free!
  • Sarah: Good job!
  • Peter: What about the tests? Shouldn’t we convert those too?
  • Me: You don’t need to. Java and Kotlin can co-exist peacefully in the same codebase.
  • Sarah: Ok, but it looks fun, I want to try it out too!
  • Peter: First let me commit.

PinGuesserTest: Convert Java File to Kotlin File and manual fixes

  • Sarah: So I open PinGuesserTest.java and run the command. How is it called?
  • Peter: Convert Java File to Kotlin File .
  • Sarah: Let’s go! … I now have a PinGuesserTest.kt . It has some errors though.
  • Peter: Maybe apply the suggestion to optimize imports?
  • Sarah: Ok… It worked.
  • Me: As you see it’s not perfect, but it’s an awesome learning tool: you start with what you already know (in Java) and see it converted in what you want to learn (in Kotlin).
  • Sarah: Let me run the unit tests… I have some weird JUnit errors.
  • Me: Ok, so I understand that. Java has static methods while Kotlin has the concept of a companion object { … }. Its methods look like static methods but are a bit different. Here JUnit really wants static methods, and we need an annotation to make it happy.

https://medium.com/media/bc8ca0c6680c868f541cf148b9f4fad1/href

  • Sarah: Unit tests now work! The project is now 100% in Kotlin.
  • Sarah: commit.

Use the Kotlin standard library

  • Peter: What comes next?
  • Me: It’s possible to create List, Set and Map the traditional Java way, but the Kotlin standard library contains plenty of small utilities to streamline that, that would be my first change. Let me do it:
  • Me: That looks better. Are the unit tests still green?
  • Me: They are, let’s commit.

Replace stream() API with Kotlin stdlib

  • Me: Something else contained in the Kotlin Standard Library are functions found in the functional programming languages like .map(), .filter(), .flatmap() and much more.
  • Sarah: A bit like the Java Stream API that we are using?
  • Me: Yes, like this but less verbose and more performant under the hood.

https://medium.com/media/3e3dd35be9483c375fb28b1fc35a725e/href

  • Sarah: Unit tests are still green.
  • Sarah: commit.

Default to read-only properties

  • Me: Next, in idiomatic Kotlin style we tend to use val property instead of var property most of the time.
  • Peter: What’s the difference?
  • Me: val property is read-only, it has no setter, it’s like a final field in Java.
  • Peter: I see. So, I just change the var property with a val?
  • Me: Pretty much so.
  • Peter: Easy enough.
  • Peter: commit.

Fail fast

  • Sarah: Is there an idiomatic way to validate the parameters of a function? The PIN should be something like 7294 with all the characters being digits.
  • Me: Yes, you use require(condition) { “error message” } for that.
  • Sarah: How would that look here?

https://medium.com/media/62aa4889ee60c55477f1ba15f5338fb8/href

  • Sarah: Thanks!
  • Sarah: commit.

Functional style

  • Sarah: What comes next?
  • Me: I would like to liberate the functions.
  • Peter: What do you mean?
  • Me: Look, we have this PinGuesser class, but what is it doing exactly? It’s doing nothing, it’s a dumb namespace. It’s a noun that prevents us for accessing directly the verbs who are doing the real work. One of my favorite programming language of all time is Execution in the kingdom of nouns by Steve Yegge.
  • Sarah: I know that rant, pure genius ! So, how do we free up the verbs/functions?
  • Me: We remove the class and use top-level functions.

https://medium.com/media/7aec10545a227af1b465b1e95b90ed7f/href

List.fold()

  • Peter: Can we go a step back? What does it bring us to make the code nicer like this? At the end of the day, the customer doesn’t care.
  • Me: Well, I don’t know you, but I often don’t really understand the code I’m supposed to work on. I tend to work hard to simplify it and, at some point, it fits in my head and the solution becomes obvious.
  • Peter: What would it look like here?
  • Me: Now that the code is in a nice functional idiomatic Kotlin, I realize that the program can be solved using a single functional construct: List.fold().
  • Sarah: Show me the code.

https://medium.com/media/0e524b9762fbb7c381d57f0df7bd1d9c/href

And we are done

I hope that you liked this article.

If you want to get in touch, you are welcome to do so via https://jmfayard.dev/

The code is available at https://github.com/jmfayard/from-java-to-kotlin.

Start in the java branch and compare with what is the kotlin branch. See this pull-request.

If you are interested to learn more about Kotlin, I’ve written about it here.

How to learn Kotlin: browser vs IDE, books vs tutorials, for newbies and Java devs

Click 👏 to say “thanks!” and help others find this article.

To be up-to-date with great news on Kt. Academy, subscribe to the newsletter, observe Twitter and follow us on Medium.

If you need a Kotlin workshop, check how we can help you: kt.academy.


From Java to Kotlin in 20 minutes ⚡️ was originally published in Kt. Academy on Medium, where people are continuing the conversation by highlighting and responding to this story.

Continue ReadingFrom Java to Kotlin in 20 minutes ⚡️

End of content

No more pages to load