From the official unveiling of the new JetBrains project in 2011 to its first stable release in 2016, the Kotlin project has been getting a lot of hype from the developer community for being a compact, minimalistic, and a very powerful programming language. It has been adopted by a lot of applications and it’s supported by some of the giants that we know today. Even though it has become a first-class citizen for writing applications on some platforms, I keep wondering whether that’s the real reason to consider completely replacing other JVM codebases and start using this language that aims to be “a better Java”? Will this programming language endure in the endless pool of languages that already serve their purpose in the programming world?
While this is not another Kotlin vs. Java post, I will try to give some opinions on how developers can benefit from the power of Kotlin and increase their overall productivity.
First and foremost, what are the benefits of using Kotlin?
- Kotlin bytecode generation means that it can interoperate with existing JVM Languages (Java, Scala, Groovy etc.), allowing us to easily integrate Kotlin into existing programs just by writing new modules and code in Kotlin, and then incrementally upgrading the existing codebase while still having a working software available at any time.
The Null safety – One of the best features so far is the avoidance of the null pointers where we must specifically declare that a variable or method can receive null values, otherwise the compilation will fail. The compilation failure will enable us to fix those errors so they won’t occur on runtime.
Unfortunately, this is not how things are done in the real world. In the real world we mostly work with Objects that have nullable fields. Let’s say we host a web server and expect the users to populate the database with data. We can’t be sure that the users will enter data into all the fields, and forcing them to do so is sometimes intrusive and causes bad user experience.
By doing this, we end up having to allow NULL values even though on the inside we deeply regret the decision (sorry dream world :’( ). By the time we finish creating our application, we can see that most of our code is actually written to prevent runtime exceptions. Kotlin can really help with reducing the boilerplate code for null checking, making the code more concise and easier to understand.
This, in turn, brings us to Safe calls, a feature designed to help with null safety that should be used by all of us who are tired of guarding against null values in multiple nested conditional statements. For this feature, Kotlin adds the safe call operator (`?.`) which can help programmers to avoid some of the Null Pointers, especially when chaining object properties.
Example: Let’s assume that we have an object of type Worker and we need to get this workers employer location.
Even though in the end we must check whether the final Location object is null or not, we still write code that is easier to understand and maintain, making it less prone to errors.
Angular users also have this feature under the name “Safe Navigation Operator”, where it’s used to avoid exceptions for null and undefined values in property paths in the HTML. This feature comes in handy when guarding against runtime errors which stop the application from executing.
Generally, Kotlin provides a more concise way to develop a more readable and maintainable code base. This can make a lot of difference for massive applications that somehow always tend to fail in performance in their later stages.
This happens mostly because of bad programming habits and the overall dependencies and complexity between the application modules.
Other times it may be due to client’s change requirements which are not estimated properly and are not taken into account when designing the software architecture. Add the sake of having working software and the pressure of the approaching deadlines to the mix, and you’ll find yourself with a poorly made decision.
Nevertheless, all those little changes may have a serious impact in the longevity of the software, most probably breaking the S.O.L.I.D principles and becoming smelly. By the time we release these changes, our only thought will be when to start refactoring.
While there are many other reasons why Kotlin is getting all the hype, my favourite one is its resemblance with other popular programming languages. As a developer who is comfortable in Swift and TypeScript, I can safely say that Kotlin makes it easy for developers to switch from/to any of the above-mentioned programming languages with minimal effort. In many ways they share the same coding style, ensuring a shrunk code base which is easier and faster to maintain, using many principles such as type inference, smart casts, extension functions and null safety.
By doing this you can vastly expand your knowledge since learning a new programming language makes us inherit some of its core principles and use them in the future. For what it’s worth, Kotlin is continuously improving and in my opinion, it will justify the hype by making it a cool new language to learn.