ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Kotlin education: Beyond the basics

Manuel Vivo
ProAndroidDev
Published in
4 min readSep 30, 2017

Introduction

Kotlin is making steady progress to become the Java replacement for most developers in the world. As far as I know, it’s been widely adopted by companies to develop their main applications. From my point of view and coming from a Java background, it doesn’t have a steep learning curve. However, you still have to learn it to get the most out of it.

When embracing it in your company, it might happen that not every member of the team is on the same page. Closing the gap between different knowledge levels is important.

This article aims to collect some Kotlin topics that go beyond basic knowledge to help you in your Kotlin education.

Sequences

Whenever you are performing some operations on Collection types, consider converting your Collection to a Sequence. You can use the asSequence() operator to convert it and there are methods available to perform the inverse operation. Sequences support the same API as Collections.

In a Sequence, the elements are evaluated lazily so you’re not wasting memory creating intermediate Collections as a result of each chained operation.

Elements in Collections are evaluated horizontally whereas Sequences evaluate the elements vertically

What does it mean? Let’s see an example:

listOfGames
.map(Game::name)
.filter { it.startsWith("PS4") }
.take(2)

With a List:

  1. All the games you have in the list are going to be processed and a new list is going to be created just with the name of the games.
  2. From that list that has been created, we’re going to process each element and we’re going to filter out those elements that don’t start with PS4.
  3. That has created another new list with just the names that start with PS4. From that list, we’re going to return a new list with the first 2 elements.

As you can see, we’ve wasted so much memory and CPU trying to compute all the elements when at the end of the day, we just wanted two of them.

With a Sequence, for each element:

  1. We get the name (that is going to be stored temporarily).
  2. If the element starts with PS4, it remains in the Sequence.
  3. Repeat until we have 2 elements in the Sequence.

As you can see, it’s more efficient and it’s perfect when you don’t know the size of the list or it can be potentially big. How can you convert the code before to work with Sequences and have the same output?

listOfGames.asSequence()
.map(Game::name)
.filter { it.startsWith("PS4") }
.take(2)
.toList()

Infix notation in functions

Infix notation is a meta-language feature that allows a more readable code and brings you closer to natural language sentences. It’s often used in Kotlin libraries and DSLs to hide simple logic and replace it for human-readable keywords. Be careful when using them, if misused, they can lead to chaos and make your code difficult to understand.

Article by Arturo Gutiérrez:

Sealed classes

Good replacement for Enum classes. Sealed classes gives you even more benefits when used in when expressions as an assignment or a return: it’s going to error out if you don’t consider all the types of the sealed class.

Article by Philipe Steiff

Objects

Remember that every time you see the keyword object, the compiler is going to create a new class.

Kotlin hidden costs

Must-read articles by Christophe Beyls

Delegated Properties

Generics: in, out, where

Annotations

Article by Tomek Polański

Reflection

DSLs

Kotlin in Action is a great book, you should read it. It covers all the basics of Kotlin but also gives you some deep understanding of some topics that are going to help you understand why some Kotlin features are like that.

Some sections are free in their website. Fortunately, the section about DSLs is. Check it out!

Another article about how to create a DSL in Kotlin

Thanks for reading,

Manuel Vicente Vivo

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Responses (2)

Write a response

Education is now a very important factor. In college, I use the support of a professional essay writer It helps me to be successful in both learning and application development

--

Great article, thank you very much! Tell me what do you think, at what age can you learn programming? My child is 11 and I was looking for a tutor for him in monstereducation, but so far I have not found someone who is ready to take on the child.

--