Skip to main content

Let's get beyond null safety

Inspired by myself and perhaps some other people, who coded Java style in Kotlin. I have seen tones of articles which (mostly) highlight Kotlins null safety and nothing more. So that's it? If it was only for that I swear I would be still using Java with some null checks. Therefore, this article suggest what to use best in Kotlin as well as droping some everyday Java habits. 

What this article is not: A comparison between Java and Kotlin.  


There is a lot of new things obviously but sharing what is important for me in variables is the lateinit and lazy implementation.

In case you never initialize the variable and use it, Kotlin will throw an exception indicating that the variable has never been initialized:

Let's jump into something fun, the lazy initialization, which basically throws us into one of the super powers of Kotlin: Delegated Properties. Delegated Properties is just a way of programming in Kotlin which must (or at least has to) be used. Think of delegation as the operator which gives "authority" to a variable to use interfaces and implementations. What does this bring into benefit? Of course, you don't have to implement interfaces all the time in your classes to perform observation, or give values to a map etc. The simplest example is the lazy() function, which takes a lambda and returns a Lazy<T> which is the delegate that "magically" remembers your variable value but the memory is still not allocated. How great is that? I use lazy wherever I can: 

I suggest checking Delegation not only for this case, but even your personal use cases, it's pretty powerful. Delegation is achieved using the keyword by.


I personally believe methods is the most innovative way in Kotlin. Honestly, one could write books only with Kotlin methods and it would be a thick one. Therefore, a single article wouldn't be enough for methods, but I would state what is more important:

Extension functions:

Extension functions is a way to apply methods to already made classes as part of them. For example, if your whole project needs a method which returns the number of characters in a String times 2 (don't judge my examples 😂)  you can create a method as part of the String class without creating a whole new class which extends String

After that, you are free to apply this method in every possible String inside you module.

Lambdas, inline, noiline, crossinline functions: 

Many programming languages including Java, Python or Javascript take functions as parameters. So does Kotlin. But since Kotlin translates to Java, it might have some possible problems on the process. Since lambdas are not such new to programming by now, i wont stop at them but I suggest using them the best way that solves your problem.

Let's jump to inline functions with lambda implementation:

In kotlin perspective, there is nothing wrong with that. But Java disagrees:

There is an instance of the Function() every time I use my executeMyLambda() method. So what does the inline keyword bring? It precisely prevents that from happening:

And in the Java part:

That's a really powerful tool. While sometimes you might need the oposite of the above case, to prevent inline function for happening, so you may provide noinline keyword before the lambda and you are good to go.

On the other hand, there might be some small problems during implementing those cases. For example, when you use the return statement inside the lambda which will break our execution of the higher method. So to prevent this we need to use the crossinline keyword before lambda. After that, even deliberately, you can't make a return statement inside it:

Reified, a small bonus for syntactic sugar and rutime generics control.

Reified gives the ability to have control over generic types at runtime. I'm going to stick to the example from the official doc, with some small modifications.

Let's see how reified gives a hand to us:

So we lost a parameter, and now we can check in runtime which is the paret type of the given generic.

Default parameters: 

You are free to provide default parameters on Kotlin  for functions that not always will require some parameter:

Notice we are not providing a value for the pageSize on the first case, but we are providing another one in the next case.
Infix and Operator Overloading

Kotlin methods marked as infix basically allow you to create your own keyword which on background is a method. It has strict rules, but it's a cool feature. For example, if you need a third instance of the object that holds only one String type variable inside, which is formed by concatinating names of 2 other users, you can do this:

So how this is solved? You can directly apply the infix keyword on that plus method:

But what if we want to go further than that? The plus keyword looks nice, but not so elegant, and since we know we can concatenate two strings with the + operator, why not do it with these objects?

So all we need to do is just add the operator keyword to that plus() method and the error will leave:

Notice we must apply the right spelling for this to happen.

Scope functions: 

Note: Cases provided below, are not the only ones that can be applied to, please refer to the docs for more info.

Kotlin has a sweet way of avoiding boilerplate or builders. It has some operators which can be applied to objects directly. Notice that operations are methods by themselves, but they apply to objects. I am taking the same example for all operations below:

So instead of doing:

We can do:

I stared with let operator since I find it more useful. The lambda method inside it is of type Calendar and the method returns the lambda result, in our case Unit. Current case does not fit well with let but the message is given.


What run does, is "creating a small class environment" inside our other classes. So basically the lambda that run returns, is the context of the Calendar.getInstance() , while the return type in this case is also Unit


with operator is very similar with run operator, but the only difference is that you provide the context as a parameter and not after the instantiation. Example:


Apply perhaps is the best solution for our Calendar instantiation. In order to store the calendar value in a variable, apply comes a lot in handy if I want to reuse that instance. The lambda here returns the same as it does in run, the context, with a small difference: apply method returns a value which can be stored: 

And last but not least, the also operator. This little guy here, comes in handy when we already have some data initialized and we want to apply extra operations on them: 

Our also here, has a lambda of type Calendar! which returns Unit, but the method here returns the Calendar type, making also and apply a perfect match.

Data classes

Data classes are the sweet thing. So this is how you create a model in java:

And this is all we have to do in Kotlin:

Getters and setters included.


These were some of the many things you can do to think in Kotlin style instead of Java. This will help you clear a lot of boilerplate code as well as removing Java style of programming.  Even though Kotlin does translate to Java and is very similar, they are not the same thing. Therefore, applying these rules will come in handy in your career as a Kotlin developer.

Popular posts from this blog

What I learned from Kotlin Flow API

I used to check the docs and just read a lot about flows but didn't implement anything until yesterday. However, the API tasted really cool (even though some operations are still in Experimental state).Prerequisites: If you don't know RxJava it's fine. But a RxJava recognizer would read this faster.Cold vs Hot streamsWell, I really struggled with this concept because it is a little bit tricky. The main difference between cold and hot happened to be pretty simple: Hot streams produce when you don't care while in cold streams, if you don't collect() (or RxJava-s equivalent subscribe()) the stream won't be activated at all. So, Flows are what we call cold streams. Removing the subscriber will not produce data at all, making the Flows one of the most sophisticated asynchronous stream API ever (in the JVM world). I tried to make a illustration of hot and cold streams: Since I mentioned the word asynchronous this implies that they do support coroutines also. Flows vs…

Modularizing your Android app, breaking the monolith (Part 1)

Inspired by a Martin Fowlers post about Micro Frontends, I decided to break my monolithic app into a modular app. I tried to read a little more about breaking monolithic apps in Android, and as far as I got, I felt confident to share my experience with you. This will be some series of blog posts where we actually try to break a simple app into a modularized Android app.

Note: You should know that I am no expert in this, so if there are false statements or mistakes please feel free to criticize, for the sake of a better development. 

What do you benefit from this approach:
Well, people are moving pretty fast nowadays and delivery is required faster and faster. So, in order to achieve this, modularising Android apps is really necessary.You can share features across different apps. Independent teams and less problems per each.Conditional features update.Quicker debugging and fixing.A feature delay doesn't delay the whole app. As per writing tests, there is not too much difference about…

From Gson to Moshi, what I learned

There is no doubt that people are getting away from GSON and I agree with those reasons too. The only advantage GSON has over other parsing libraries is that it takes a really short amount of time to set up. Furthermore, the most important thing is that Moshi is embracing Kotlin support.

First let's implement the dependency:
implementation("com.squareup.moshi:moshi:1.8.0") It's not a struggle to migrate to Moshi. It's really Gson look-a-like. The only thing to do is annotate the object with @field:Json instead of @SerializedName (which is Gsons way for JS representation):

data class User( //GSON way @SerializedName("name") val name: String, @SerializedName("user_name") val userName: String, @SerializedName("last_name") val lastName: String, @SerializedName("email") val email: String ) data class User( //Moshi way @field:Json(name = "name") val name: String, @field:Json(name = "user_name…