Method count on Kotlin data class and features

Jan 30, 2018 · 3 min read

One of the most prominent feature of Kotlin showing great Java boilerplate code removal is the introduction of Data Class. It conveniently provides various functions useful when we have a model class storing some state. Why not just convert all models to Data Classes? Wait!! …

Everything has a cost

Yes, those data classes functions provided to us are almost free, but it does cost something. It does adds method counts to our APK. As you probably know, Android has a 64K method count cap limits to ensure single dex, hence we don’t want to add unnecessary methods count.

Measuring the methods counts.

To measure method count, I’m using this cool library, that gives us the method counts after each compilation.

To be reflective of a releasable App, I also turn on Proguard to ensure it optimize away the codes.

To be fair, I’m taking the example from Kotlin Data Class example

data class User(val name: String = “”, val age: Int = 0)

I would expect 1 method (perhaps the constructor), 2 fields and 1 class (obviously). Anyway, lets measure instead of assume.

Let’s start the Measurement

Original Code

data class User(val name: String = “”, val age: Int = 0)

Methods = 18
Fields = 2
Classes = 3

Oh gosh!! 18 methods, and 3 classes instead of 1. Why is it so many?

Let’s strip it down a little more

Remove the default argument

data class User(val name: String, val age: Int = 0)

Methods = 16
Fields = 2
Classes = 3

Interesting, we now have 2 methods reduce. Default argument introduced additional functions indeed.

note: I did the same for `age`, but no different with or without default parameter

Make variable nullable

data class User(val name: String?, val age: Int = 0)

Methods = 5
Fields = 2
Classes = 2

Wow, make the name nullable, we reduce the method counts by 11. Not only that, we also eliminate a class!

note: I did the same for `age`, but no different both for nullable or non-nullable

Remove the data keyword

class User(val name: String?, val age: Int = 0)

Methods = 1
Fields = 2
Classes = 1

Finally… now this matches my initial way of counting. Back to the most basic.


In short, there are some cost in term of methods and classes count when we use non-nullable, default parameter and data classes. Please don’t get me wrong. I’m not advocating that we should not use them. I like them as they make the codes more concise and also safer.

However, if they are not needed, don’t just make them one. E.g. don’t convert all model classes to data classes, but only if you need to take advantage of the data class features.

Don’t use it if you don’t need it. There’s always a cost.

I hope you appreciate this post and it’s helpful for you. Do share with others.

You could check out my other interesting topics here.

Follow me on medium, Twitter or Facebook for little tips and learning on Android, Kotlin etc related topics. ~Elye~


Written by


Passionate in learning and sharing mobile development and its adjacent technology. If you like to buy me a coffee visit Thanks!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade