Image for post
Image for post

👨‍🍳 Cooking Tasty code in Kotlin 🍴 — Part 1

Shreyas Patil
Dec 24, 2020 · 7 min read

Hello developers 👋, In this article, I’ll walk you through some basic recipes to cook your code tasty 😋 in Kotlin. You’re here in the first part of this series. I’ll present some of my views in front of you and I hope you’ll like it. This article is basically for the people who are a beginner in Kotlin, want to start development in Kotlin or people who are coming from other programming languages.

I’m working with Kotlin for 2 years as of now and in these days I’m actively working with a community of Kotlin developers. In this time span, I always noticed that the community is using Kotlin but it’s not leveraging this beautiful programming language. In short, I would say — Developers are using Kotlin programming language syntax but they’re writing code as they’re writing code in Java😶. That’s it!.

Kotlin is a really easy or friendly programming language which is expressive and concise, allowing you to express your ideas with less code. This helps in reducing the amount of boilerplate code in your project and there are so many features of Kotlin which can’t be explained in a single article 😄.

That’s enough introduction I guess and now I think we should start talking about Kotlin. So here are some Kotlin-ish concepts which I would recommend to use in your codebase 👇 and let’s take advantage of this superpower programming language.

⭐️ Function

⭐️ Single Expression Functions

It’s not necessary to mention return type of a function when we use such expression but IMO it makes code more readable for a person who’s seeing your code for the first time 😃.

⭐️ Default Argument Functions

⭐️ Named Arguments Function

⭐️ Scope Functions

In this code, we used ? operator on a person and used fun let {} which provides a lambda parameter p (it remains it if not provided explicitly). Then we can safely use that property.

let{} can be also used to obtain some value after processing. For e.g. here we are getting age from the evaluation performed in the body of a lambda.

The body of lambda of function apply{} provides this scope of instance on which we’re calling it and returns the same instance which we can use for chaining later.

There’s a lot more we can do with scope functions. Know more about Scope functions here.

⭐️ Extension Function

As you can see, we directly called date.format(“pattern”).

As extension function exists, extension properties also exist. Let’s see them.

⭐️ Extension Property

Here we created extension properties on Int which returns binary, octal and hexadecimal.

Observe carefully, we have used get() = which is invoked everytime when we’ll access the field.

⭐️ Operator Overloading

If you look, the first snippet looks bit confusing but the second snippet looks good and we get a clear idea of what’s happening. Know more about operator overloading here.

⭐️ Infix Function

Did you saw that line? task assignTo user. It’s sweet, isn’t it? 😃

Just mark a function as infix and you can create your own sweetness in your codebase. You can even cook beautiful code by using Extension Function + Infix Function together ♨️.

⭐️ Inline Function

Now as you can see, we have marked processList() as inline. Now see generated bytecode here 👇 and you’ll see that whatever we’ve written in inline function is exactly present in main() function.

Image for post
Image for post

⭐️ Reified type parameters in Inline Function

As you can see, now the type of class is accessible inside the function, almost as if it were a normal class. Since the function is inlined, no reflection is needed. Otherwise, without reified we might need to use reflections to deserialize that JSON.

⭐️ Typealias

⭐️ Import As

Here, we have imported java.util.Date class as JDate and we can use it by using that name only in this file.

Yeah! 😍 That’s how we cooked tasty code with Kotlin in this first part of this article and I hope you liked this article. In this article, we saw some basic important concepts of the Kotlin. In the next article, we’ll see some advanced one. Stay tuned for the next one 🚀.

Next part of this series is available 😍🔽

If you liked this article, share it with everyone! 😄

Sharing is caring!

Thank you! 😃

This article has been featured in AndroidWeekly and KotlinWeekly newsletters. Thanks!


We build applications that scale beyond reality

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store