News Project-Based Learning

Learn in 1 Hour: 4 Tips to Write Less Code in Kotlin

Time flies so fast — it’s already the middle of the Hour of Code week! We are so proud of your commitment and continuous curiosity about learning. Even though programming can be challenging, the hours you spend on it add up to the most amazing experience. So let’s keep the ball rolling! The next hour at JetBrains Academy will be about one of the top 5 fastest-growing languages — Kotlin.

Kotlin is a highly effective modern programming language that can be used for any kind of development, be it server-side, client-side web, or Android. The main philosophy behind Kotlin development is to make a language that is as close to human thought as possible. Oftentimes, reading code is much more important than writing it, and that’s why Kotlin tries to remove most of the noise in the code to make it as readable as possible. And as an amazing bonus, it also significantly reduces the size of your code.

Our estimates show an approximately 40% cut in the number of code lines compared to Java. Besides, Kotlin is more type-safe, and fewer exceptions and errors also mean less code for you to work with.

Let us introduce you to 4 educational topics that will teach you to write less code with Kotlin in just an hour:

🕐 Extension functions: Usually, developers don’t write their programs completely from scratch but use libraries or pre-written classes to save time. And more often than not, there’s no way to change or edit third-party code. That’s where extension functions come to the rescue! They allow you to add more functionality to the existing classes without modifying them. 

🕐 Data class: Kotlin provides a useful tool for situations where you purposefully need a class to hold data. This class basically allows you to create a plain old Java object with setters, getters, hashCode(), toString(), equals(), and copy() in just a single line. It automates a very common use case of having a class that holds data and may be manipulated as a single unit without adding any extra logic to it. 

🕐 Destructuring declarations: Destructuring declarations are a convenient feature that allows creating multiple variables at once. It is a helpful tool for when you need to destructure an object into a number of variables to better manipulate it or to make a piece of your code more concise. 

🕐 Null safety: One of the most common pitfalls in many programming languages, including Java, is that accessing a member of a null reference will result in a null reference exception. So you need to constantly check if something is null before you use it to avoid this “billion dollar mistake”. Kotlin’s type system, on the other hand, is aimed at eliminating the danger of null references and makes sure you don’t end up with the Null Pointer Exception unless you really want it.

Let’s keep each other motivated during this week! Share a story about some interesting, challenging, or fun 1-hour experience you’ve had at JetBrains Academy and get a chance to win a 25% discount on your personal subscription at JetBrains Academy. Learn more about the rules in the Hour of Code at JetBrains Academy blog post.

Happy Hour of Code!
Your JetBrains Academy team

image description