for server side

Our plan today

Kotlin mascot
  1. What is it ?
  2. Why using it ?
  3. Google journey
  4. Conclusion

1. What is it ?

Statically typed language created by Jetbrains

First commit 2010

Open Source: https://github.com/JetBrains/kotlin

(Apache License)

Pushed by Google as the recommended language for Android since 2019

(Java before)
A language under influence

Takes the best from other languages

A language under influence

Takes the best from other languages

C# - Groovy - Scala - Swift - Java

Multiple compilation target
Multiple compilation target
  • Compiled into Java bytecode and run on a JVM
Multiple compilation target
  • Compiled into Java bytecode and run on a JVM
  • Transpiled to EcmaScript (JavaScript)
Multiple compilation target
  • Compiled into Java bytecode and run on a JVM
  • Transpiled to EcmaScript (JavaScript)
  • Compiled to native binaries
Multiple compilation target
  • Compiled into Java bytecode and run on a JVM
  • Transpiled to EcmaScript (JavaScript)
  • Compiled to native binaries
  • WebAssembly
Who use it on server-side ?

adobeawsatlassianshazam

and many more

2. Why using it ?

Why bother with another language ?

211000 NullPointerException on GitHub

19600 duplicates on StackOverflow

antony_hoare

“I call it my billion-dollar mistake. It was the invention of the null reference in 1965. Which have probably caused a billion dollars of pain and damage in the last forty years.”

Tony Hoare

Null reference Java vs Kotlin

                    
                        // Java
                        final String nullableString = null;
                        nullableString.lenght(); // code perfectly valid & compiled

                        // Kotlin
                        var nullableString: String? = null
                        nullableString.length // compiler error, not allowed
                        var notNullableString: String = "Null reference is forbidden"
                    
                
Designed to avoid entire classes defects
Java Kotlin
Nothing enforced at type system Nullability built into the type system
No check at compile time Full check at compile time
NullPointerException at runtime 🤞 No NullPointerException 🥳
Optional only usable for return values Functional constructs on nullable value
Magic syntax sugar

bye bye Lombok and Apache Utils 👋

Destructuring declarations

                    
                        // Java
                        final var firstname = person.getFirstname();
                        final var lastname = person.getLastname();
                        final var age = person.getAge();

                        // Kotlin
                        val (firstname, lastname, age) = person
                    
                

Methods overloading vs named arguments

                    
                        void log() {
                            log("Hello world", LogLevel.INFO);
                        }

                        void log(String message) {
                            log(message, LogLevel.INFO);
                        }

                        void log(String message, LogLevel level) {
                            log(message, level);
                        }

                        void log(String message, LogLevel level, Object... args) {
                            // since Java 14
                            return switch (level.name()) {
                                case "INFO" -> log.info(message, args);
                                case "ERROR" -> log.error(message, args);
                                default -> throw new IllegalArgumentException(
                                    "Unrecognized level " + level.getName()
                                );
                            };
                        }
                    
                

Methods overloading vs named arguments

                    
                        fun log(message: String = "Hello world", level: LogLevel = LogLevel.INFO, vararg args: Any) {
                            when (level.name) {
                                "INFO" -> log.info(message, args)
                                "ERROR" -> log.error(message, args)
                                else -> throw IllegalArgumentException(
                                    "Unrecognized level ${level.name}"
                                )
                            }
                        }
                    
                

Utils vs Extension functions

                    
                        // Java
                        final class NumberUtils {
                            public static boolean isEven(final int i) {
                                return i % 2 == 0;
                            }
                        }
                        NumberUtils.isEven(2); // later in the code

                        // Kotlin
                        fun Int.isEven() = this % 2 == 0
                        2.isEven() // later in the code
                    
                

And a lot more...

Interoperability
meme

100% interoperable with Java code

                    
                        // Kotlin
                        class AccountingService {
                            fun addTwoNumbers(a: Int, b: Int) = a + b
                        }

                        // Java
                        public class Main {
                            public static void main(String[] args) {
                                final AccountingService accountingService = new AccountingService();
                                int result = accountingService.addTwoNumbers(5, 7);
                            }
                        }
                    
                

and vice-versa 😎

                    
                        // Java
                        public class AccountingService {
                            public int addTwoNumbers(int a, int b) {
                                return a + b;
                            }
                        }

                        // Kotlin
                        fun main() {
                            val accountingService = AccountingService()
                            val result = accountingService.addTwoNumbers(3, 4)
                        }
                    
                
Readability

Less boilerplate code

40% cut in the number of lines of code

Less code = less cognitive effort

Easier code to read is easier to maintain

Smooth learning curve

Few hours to read - Couple of days to produce

Makes it easier to integrate a project for newcomers

Largely Supported

Kotlin seamlessly integrates with various frameworks, offering developers flexibility and efficiency

springquarkusmicronautvaadin

spring Framework actively supports Kotlin, allowing developers to leverage the strengths of both technologies.

Actively supported since Spring 5 (2017)

Documentation available with both Java & Kotlin examples

3. The Google example

Adoption of the language at a large scale

Worth it or not ?

Java really well implemented at Google for many years

Kotlin introduced as Java alternative for their server-side projects 6 years ago

Result: Kotlin adoption is growing quickly

Studies around the impact of the language internally
arrow

70% of devs whose primary language is Kotlin think it makes them more productive

(faster to write & language features)

87% of Google server side Kotlin devs are very satisfied with it

(easy on ramp, incremental learning & adoption)

StackOverflow survey

20% less likely to crash

(Null safety, more correct code is easier to write)

0% degradation of productivity in mixed Java/Kotlin codebase

(incremental adoption, great interoperability)

4. Conclusion

meme
To summarize
  • Makes bad choices explicit or impossible
  • Less defects
  • Incremental adoption (interoperability)
  • Less code & code is more expressive
  • Great integration with tools (Maven, Spring...)
  • Elegant & beautiful 😍
A lot more cool features

(which we won't have time to cover today) 🕜

  • String interpolation
  • Structural equality / referential equality
  • Smart casting
  • Type inference
  • Coroutines
  • and more...
Drawback(s)

Bytecode generated at the compilation

Compilation can be slightly longer than what it is for Java

References & links

Some videos:

The git repository used for the analysis:

Some articles:

Questions or comments ?