Why Kotlin makes developers live happier lives…
If you are a pure Java Developer and you want to make your life easier and more pleasant — then start using Kotlin!
The main reasons behind creating this new programming language were to incorporate all the features of other mature languages in one place, to run in the Java Virtual Machine and to be compiled as fast as Java. To cut a long story short, the Kotlin language has been invented as a result of taking into consideration these three criteria. What is more, it was named — in line with convention — after one of the islands near Saint Petersburg. The first sentence of Kotlin’s documentation says „Kotlin is a modern but already mature programming language aimed to make developers happier” — is this true? Let’s find out!
Similarities to the Java language
As mentioned above, Kotlin is designed for the Java Virtual Machine and has really good compatibility with other JVM languages. It hasn’t got its own libraries for backend applications, so it uses the ones from Java and extends them with extra and easy-to-use features. According to Spring’s documentation, Kotlin developers can benefit from Spring and Spring Boot because it supports both these frameworks.
Kotlin is similar to Java in that it is a static language, which means that the type of object is known only during the compilation process. Furthermore, Kotlin uses smart type casting so you don’t have to explicitly determine the object type — it is automatically done based on the context.
Benefits of using the Kotlin language
Below, we’ll take you down the path of Kotlin’s benefits, exploring some of the language’s most useful features and how they can improve your code. From data classes and null safety to extension functions and concise syntax, Kotlin offers a range of advantages that make it an increasingly popular choice for developers. So, let’s dive in and discover what Kotlin has to offer.
Data classes
What is worth mentioning is that Kotlin has got data classes (example no 1) — objects which allow immutable instance creation. Additionally, methods such as toString(), equals() and hashCode() are not needed to be overridden, because it is made out of the box. A similar approach was used in Java 11 as record classes.
Null safety
Kotlin makes us get rid of NullPointerExceptions. Sounds great, doesn’t it? But how can we achieve it? Foremost, we should know that Kotlin has two types of references — nullable and non-nullable. If you are working with a nullable one you just need to add a question mark („?”) at the end of the declaration type (example no 1). As a result of this you are able to ensure null safety implementation and prepare your application to handle nullable values. The case of creating an instance with a nullable field is presented in example no 2. In this illustration, there is also an Elvis operator applied “?:” which could return a default value or throw an exception.
If we would like to call a method on a nullable field, we just need to use the “safe call” approach — add a question mark or two exclamation marks (when we are sure that the value is not null) after the field’s name (example no 3).
*As you probably noticed, Kotlin doesn’t require semicolons at the end of the line. Moreover, the attribute declaration is not the same as it is in Java. Firstly, there is a `val` (value) or `var` (variable) expression, then the name of the field, next „:” char and nullable or non-nullable type declaration.
The approach of getting rid of NullPointers in Kotlin is better than adding @Nullable and @NotNull annotations as it is in Java, because you don’t have to compile the code to see if all nulls are handled. Moreover, any wrapping objects like Optional (Java) are not needed.
The extension function
The second benefit of using Kotlin language is the feature of the extension function. This is a method which is declared outside the class, but can be called as if it were a part of it. We can say that it behaves as a Decorator pattern, because you can add new features to the object without modifying it. Examples of this are presented below (no 4 and no 5).
The inline function
Furthermore one of the biggest advantages of using Kotlin is the fact that we can reduce code. A lot of things are made out of the box, which helps us to keep code clean, transparent and without boilerplate code. Thanks to that, such code is easy to understand and go through. The feature that could help us in acquiring a more concise code is the inline function — instead of applying braces and a `return` expression, „=” char is used (example no 6).
Below, there are examples (no 7.) of calling inline functions with default values — another great feature, which could be especially useful during writing tests. If no value has been given in a method’s argument, the default value will be assigned. What is more, variations of calling a function with different arguments are shown in example no 7. In this case, in Java overloaded functions should be applied.
Scope functions
In order to make our code more concise we can use let/also/with structure from the Kotlin standard library. These forms are examples of scope functions in which we can execute a code within a context. The differences in these discussed expressions are the result of construction and the way of how to access the object. Let and also are from a technical point of view extension functions but with takes the context object as an argument. There are some basic rules as to when we should use these closures — with to group functionalities, let to execute a lambda on non-null objects and also to provide some additional effects. If you are not convinced that it helps us to produce more readable code and increase its clearness then look at the examples below (no 8 and no 9).
Kotlin syntax assure fewer lines of duplicated code. Fewer lines of well written code means fewer bugs. Fewer bugs equals happier developers. Are there more advantages of using Kotlin? Yes!
Java and Kotlin code can be used in the same project and work in parallel. It could be significant information for those of you that would like to introduce Kotlin to specific parts of your project or just to provide a new language step by step, class by class.
Conclusion
If you feel lost, don’t worry! Any secret skills or extra tips are not needed to call Kotlin’s function, because it imitates Java. The more Kotlin code you write, the faster you gain new syntax habits. Don’t waste your time writing boilerplate code as it is in Java. Start using Kotlin and provide more concise code to your project. The time you will save thanks to all of Kotlin’s benefits (interoperability, safety, clarity) — you can spend on something that will make your life happier! :)