Davide Cerbo

Feb 15, 2020

4 min read

Can Kotlin do it better?

In this post I will do a simple refactoring of a real small piece of code that I found in a legacy code base and after I will translate the example to Java to Kotlin. Why? Only in order to demonstrate why Kotlin can be useful for writing more maintainable code and not only a fashionable language for old Java developer, like me :)

Every day we face simple problems like splitting a list into two new lists.

Ok, above we solve our problem, but our code is doing too much stuff and its hard to change when the requirement will change. Moreover, we are not trendy because we are not using at Java Stream (Java 8 it is released in 2014, come on!!!) to iterate our list. So we can refactor our code in:

But nothing is changed, we just use a different API, but our code logic is still the same.

PS: Always write a test before starting the refactoring in order to validate that the original behavior doesn’t change, otherwise a kitten will die!

Serious refactoring in Java

Even though write less code is better, we are doing two things in the same piece of code, so we can make our code cleaner, just filling a list at a time.

After we can extract a method to avoid duplications and to have a decent code.

Now, using our shining new method extractFrom its very simple to add new features in our method, for instance, if we want to collect adult users, we can simply add a line in our code.

We already moved some responsibilities from one method to another, but, we can move the responsibility about the type of the credential, or if the user is Adult in the Credentials class and use a cool method reference:

We can do more refactoring? Of course! But for our target, it is enough.

Kotlin can do it better

In Koltin we can do even better, as shown below:

The code above is exactly the same that we did in Java, but using some Kotlin features:

  • Implicit name of a simple parameter: we can remove unuseful code using the keyword “it” and improving code readability. This keyword stands for the default single parameter of a function, so we don’t have to declare it explicitly.
  • Passing trailing lambdas: in Kotlin we can pass a lambda expression outside function parameters when the last parameter of a function is a function, and if it is the only argument, the parentheses can be omitted entirely.
  • Data classes: are classes whose main purpose is to hold data and they provide useful methods like copy that can create a new copy of the object. The method accepts as parameters any field of the class, using named arguments, so we can customize our copy, reaching immutability principle, because of our data class is immutable because it will use val modifier to define class fields:

Further reading

In the past, I wrote two articles like that, unfortunately, they are in Italian, but there is a lot of code, so you can understand them even just by reading the code ;)