179

Why and how I switched to Kotlin – AndroidPub

 6 years ago
source link: https://android.jlelse.eu/why-and-how-i-switched-to-kotlin-503460bbfcf
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Why and how I switched to Kotlin

I’m one of the very few (judging from my twitter stream) who weren’t jumping up and down out of excitement when Google announced Kotlin as a first tier language in Android development. Let me explain why?

Why I resisted

I remember listening to Java Posse podcast some 10-or-so-years ago when Tor Norbye and Dick Wall were talking about the importance of learning a new programming language as often as possible. While I see the point, it never felt to me as the right thing to do. As a kid, I was programming in Pascal and dabbled in Basic but since starting in the University of Helsinki CS I jumped into Java and was a Java developer ever since. I spent 20 years of my life writing Java for web, backend, desktop, web-frontend, and of course lately for Android. Java became a second nature to me. It was easy. I never saw a need for anything else. I even resisted bringing new Java language features to my coding because, why would I need them?

I believe I’m not alone. I don’t think programmers need to learn new languages constantly. Instead, there’s a need to become bloody good at the language you use to minimise bugs and to maximise productivity.

Other reason for not being interested in Kotlin was the annoying politics and legal battles happening around Java with Google and Oracle. Until the announcement I couldn’t help but feel the history repeating. Jump into Kotlin, wait an a-hole company to buy the language owner and there we go again.

Combine that with the fact that Google had multiple languages under works internally already. Maybe Kotlin weren’t going to be the chosen one.

After the announcement everything changed. Kotlin was moved to a non-profit not controllable by an ill-willing company. It was safe. Google’s dedication was clear and the tools were already there (JetBrains have been working on Kotlin tooling for years).

It was time to make the jump.

My learning process

I started from the very beginning of never having even seen Kotlin syntax. So, to start, like with almost everything, I typed “Kotlin” into YouTube search engine.

Step 1- Kotlin Tutorial by Derek Banas

The video that caught my eye was the Kotlin Tutorial by Derek Banas:

This is a great starting point for anyone to get going with Kotlin

In fact, I ended up watching the video twice end to end to get overall understanding of what Kotlin was about. This phase of learning was all about understanding the context. When learning anything detached facts are difficult to remember but information in right context is easy to understand. I needed to prime my personal context engine so I can start learning on full.

It’s worth noting that I did try the Koans at this point but that wasn’t the way to go for me. I didn’t like that approach.

Step 2- Kotlin for Android Developers by Antonio Leiva

This book got me up to speed

Now I was ready to start learning and understanding how Kotlin will work in my day job as an Android dev.

I bought Antonio Leiva’s book “Kotlin for Android Developers”.

This book is great. It assumes that you know Android development already but that’s it. It helps you to understand the basics in your own, and familiar, context.

While I’m not huge fan of the database examples in the book (I don’t work much in that part of Android so the examples didn’t fit my experience) the rest is exactly what you need to get going.

While reading I did play around in Android Studio a little bit writing some simple classes and Activities and familiarised myself with the AS Kotlin integration.

Step 3- Kotlin language reference

Kotlin language reference is exceptionally well written. It’s without fluff and it has everything. I downloaded it as a PDF from JetBrain’s website, uploaded to my Google Books and took a comfortable position on our office couch with my Chromebook and read it.

Now that I understood the context of the language (and had dabbled with some of it in AS) the reference documentation made a lot of sense. Every new concept I read about felt connected to something I had already learned.

The first readthrough I didn’t even try to memorise any of it. I just consumed it hungrily to understand the size of the language.

In the end I ended up reading the reference through 3 times. On every readthrough more of it stuck.

Of course, at this point I was also working on a hobby project already writing it in Kotlin from ground up.

Step 4- Hobby project

Android Studio’s Kotlin integration is amazing. JetBrain has been working on this for years already in their IntelliJ IDE which AS is based on.

I love the fact that I can copy an example in Java from web (SO) and paste it to my Kotlin project and the IDE will perform a conversion for me.

The best part is the Android Studio’s quick helps when it detects that your code could be written better. Once I had the Kotlin basics down I simply kept on working on my hobby project and whenever AS noticed that I did something suboptimally I learned something new!

An example of me using initialiser ineffectivelyMe not marking an unused variable as such

Conclusion on learning

It took me about 3 weeks to get to this point. After the step 4, you probably won’t be ready to jump in unguided to a customer project.. I wasn’t. But you won’t be far off either. You’ll certainly be ready to work in a project under guidance from more experienced Kotlin coders. Personally, I recommend getting your code reviewed by someone who can detect potential issues and teach you some of the more neat tricks.

Next steps?

I’m looking for the nexts steps. I read a lot of blog posts written by experienced devs and watch new material popping up in YouTube around the topic. However, what I’m really missing is some much more advanced level content about Kotlin in Android context. I want to learn about best practices. Kotlin is a powerful language and it allows devs to solve problems with multiple different ways. It’ll take time before we find the best ways and develop defacto standards in Android devving.

Do you know any great resources for advanced Android Kotlin? If so, please leave a comment below!

Small business point of view

I’m one of the founders of Snapp Mobile. We write mobile apps for our customers (and sometimes for ourselves). We’re a small company running only couple projects at once. For us, as a company, the decision to jump was relatively easy. We’ve seen first hand the difficulties iOS legacy projects written in ObjC cause to our customers. There’s no way we want that to happen in Android projects we’re involved. We jumped on Swift on iOS immediately when it became a stable language as that was clearly the way forwards. On Android Kotlin is the same thing.

While Java is much more widespread language than ObjC ever was I believe we’ll see a similar drop off of interest in the dev community Java as we saw with ObjC. Soon, you’ll have to pay extra to find devs who are still willing to work in the legacy environment.

So, we did the jump as a business already as well. We write all of our new projects in Kotlin and in Kotlin only. We’re also slowly, time permitting, converting our internal projects into Kotlin to allow us to have only one language to maintain in the future.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK