Kt. Academy Logo
WorkshopsBooksArticlesStore

Effective Kotlin: Best Practices

Kotlin is a powerful and pragmatic language, but it's not enough to know about its features. We also need to know when they should be used and in what way. This book is a guide for Kotlin developers on how to become excellent Kotlin developers. It presents and explains in-depth the best practices for Kotlin development. Each item is presented as a clear rule of thumb, supported by detailed explanations and practical examples.

Effective Kotlin provides insights into the idiomatic way of Kotlin development, as well as many general programming best practices and details about advanced Kotlin features like DSL creation, generics, platform types or inline functions and classes. All that for experienced Kotlin developers to get more effective when they use this programming language: to make safe, well designed and efficient code.

The purpose of this book

To really unleash the advantages of Kotlin, we need to use it properly. To do so, not only do we need to know about different Standard Library (stdlib) functions but even more, we need a good understanding of Kotlin features, purpose, and design. The main goal of this book is to explain how to use different Kotlin features to achieve safe, readable, scalable, and efficient code. Since this book is written to help developers get better at writing code, it also touches many general rules for programmers. This book tries to compose as much knowledge about best practices in Kotlin as possible. You can call it a collection of best practices.

For whom the book is written

This book is not teaching basics. It assumes that you have enough knowledge and skills to do Kotlin development. If you don’t, I recommend starting first from resource designed for beginners.

Effective Kotlin is directed to experienced Kotlin developers. Though I will assume that even experienced developers might not know some features. This is why I explain some concepts like:

  • Property
  • Platform type
  • Named arguments
  • Property delegation
  • DSL creation
  • Inline classes and functions
  • Tail recursion

I want this book to be a complete guide for Kotlin developers on how to become an amazing Kotlin developer.

Main parts of the book

Concepts in the book are grouped into three parts. Each part is divided into chapters, which are subdivided into item. Those parts are:

Good code

More general rules about making good quality code. This part is for every Kotlin developer, no matter how big their project is. It starts from items about safety and later talks about readability. It is not a coincidence that the first chapter is dedicated to safety. I believe that program correctness generally is of the highest priority, and safety is an important component. Readability is another item because the code is not only for a compiler but also for programmers. Even when we work alone, we want code that is readable and self-explanatory.

Code design

This section is for developers creating a project together with other developers, or creating libraries. It is about conventions and setting contracts. It will, in the end, reflect on readability and safety, but all in terms of correct code design. This part is a bit more abstract at the beginning, but thanks to that it can explore topics that are often omitted in books about code quality. This section is also about preparing our code for growth. A lot of items are about being ready for changes in the future. Therefore it is an especially important section for developers creating large projects.

Efficiency

This section is for developers that care about code efficiency. Most of the rules presented here do not come at the cost of development time or readability, so they are suitable for everyone. However, they are particularly important for developers implementing high-performance applications, libraries, or applications for millions.

Table of contents

Introduction: Be pragmatic

Part 1: Good code

Chapter 1: Safety

  • Item 1: Limit mutability
  • Item 2: Eliminate critical sections
  • Item 3: Eliminate platform types as soon as possible
  • Item 4: Minimize the scope of variables
  • Item 5: Specify your expectations for arguments and state
  • Item 6: Prefer standard errors to custom ones
  • Item 7: Prefer a nullable or Result result type when the lack of a result is possible
  • Item 8: Close resources with use
  • Item 9: Write unit tests

Chapter 2: Readability

  • Item 10: Design for readability
  • Item 11: An operator’s meaning should be consistent with its function name
  • Item 12: Use operators to increase readability
  • Item 13: Consider making types explicit
  • Item 14: Consider referencing receivers explicitly
  • Item 15: Properties should represent a state, not a behavior
  • Item 16: Avoid returning or operating on Unit?
  • Item 17: Consider naming arguments
  • Item 18: Respect coding conventions

Part 2: Code design

Chapter 3: Reusability

  • Item 19: Do not repeat knowledge
  • Item 20: Do not repeat common algorithms
  • Item 21: Use generics when implementing common algorithms
  • Item 22: Avoid shadowing type parameters
  • Item 23: Consider using variance modifiers for generic types
  • Item 24: Reuse between different platforms by extracting common modules

Chapter 4: Abstraction design

  • Item 25: Each function should be written in terms of a single level of abstraction
  • Item 26: Use abstraction to protect code against changes
  • Item 27: Specify API stability
  • Item 28: Consider wrapping external APIs
  • Item 29: Minimize elements’ visibility
  • Item 30: Define contracts with documentation
  • Item 31: Respect abstraction contracts

Chapter 5: Object creation

  • Item 32: Consider factory functions instead of secondary constructors
  • Item 33: Consider a primary constructor with named optional arguments
  • Item 34: Consider defining a DSL for complex object creation
  • Item 35: Consider using dependency injection

Chapter 6: Class design

  • Item 36: Prefer composition over inheritance
  • Item 37: Use the data modifier to represent a bundle of data
  • Item 38: Use function types or functional interfaces to pass operations and actions
  • Item 39: Use sealed classes and interfaces to express restricted hierarchies
  • Item 40: Prefer class hierarchies instead of tagged classes
  • Item 41: Use enum to represent a list of values
  • Item 42: Respect the contract of equals
  • Item 43: Respect the contract of hashCode
  • Item 44: Respect the contract of compareTo
  • Item 45: Consider extracting non-essential parts of your API into extensions
  • Item 46: Avoid member extensions

Part 3: Efficiency

Chapter 7: Make it cheap

  • Item 47: Avoid unnecessary object creation
  • Item 48: Consider using object declarations
  • Item 49: Use caching when possible
  • Item 50: Extract objects that can be reused
  • Item 51: Use the inline modifier for functions with parameters of functional types
  • Item 52: Consider using inline value classes
  • Item 53: Eliminate obsolete object references

Chapter 8: Efficient collection processing

  • Item 54: Prefer Sequences for big collections with more than one processing step
  • Item 55: Consider associating elements to a map
  • Item 56: Consider using groupingBy instead of groupBy
  • Item 57: Limit the number of operations
  • Item 58: Consider Arrays with primitives for performance-critical processing
  • Item 59: Consider using mutable collections
  • Item 60: Use appropriate collection types

Dictionary

Marcin Moskała

Marcin Moskała

Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."

Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.

Chapters available as articles

Effective Kotlin Item 1: Limit mutability

Why it is so important to limit mutability, and how Kotlin supports it.

Effective Kotlin Item 2: Eliminate critical sections

Learn why do we need to synchronize access to mutable state, and how to secure it.

Effective Kotlin Item 5: Specify your expectations for arguments and state

How do we specify requirements and expectations in Kotlin.

Effective Kotlin Item 7: Prefer a nullable or Result result type when the lack of a result is possible

Why should we prefer to avoid throwing exceptions and using types to our advantage.

Effective Kotlin Item 10: Design for readability

Do we really want out applications concise, or do we rather want them readable?

Effective Kotlin Item 19: Do not repeat knowledge

Why knowledge repetition is so problematic and how it relates to the single responsibility principle.

Effective Kotlin Item 20: Do not repeat common algorithms

Why extracting utility functions is so important for our programs.

Abstraction design: Introduction

What is abstraction in programming and why is it so important.

Item 25: Each function should be written in terms of a single level of abstraction

Why each function should be written in terms of a single level of abstraction and how to achieve it.

Item 26: Use abstraction to protect code against changes

How do we abstract elements, and what are the consequences.

Item 27: Specify API stability

How do we specify API stability and why it is so important.

Item 28: Consider wrapping external APIs

Why we should wrap external APIs and how to do it.

Item 29: Minimize elements’ visibility

Why we should minimize elements’ visibility and how to do it.

Item 30: Define contracts with documentation

How do we express our library or module contracts and why are they so important.

Item 31: Respect abstraction contracts

Just because we can do something, does not mean we should. How to respect abstraction contracts and why it is so important.

Effective Kotlin Item 32: Consider factory functions instead of secondary constructors

What factory functions are and why they are so important patterns for object creation.

Effective Kotlin Item 35: Consider using dependency injection

What is dependency injection, why it is so important, and how to use it in Kotlin.

Effective Kotlin Item 36: Prefer composition over inheritance

Years of OOP made us overuse inheritance. Instead, we should more often use a composition that is safer and more explicit. More often, but not always...

Effective Kotlin Item 46: Avoid member extensions

What member extensions are, how they are possible and why we should avoid using them.

Effective Kotlin Item 47: Avoid unnecessary object creation

About the most essential rule of performance optimization.

Effective Kotlin Item 48: Consider using object declarations

Why we should use object declarations instead of regular classes.

Effective Kotlin Item 49: Use caching when possible

Why we should use caching and how to do it.

Effective Kotlin Item 51: Use the inline modifier for functions with parameters of functional types

How inline functions work and why they can be so important for the performance of our application.

Effective Kotlin Item 52: Consider using inline value classes

What value classes are, how to use and inline them.

Effective Kotlin Item 53: Eliminate obsolete object references

How to help our garbage collector and avoid memory leaks.

Effective Kotlin Item 54: Prefer Sequences for big collections with more than one processing step

What the difference between list and sequence processing is, and when each should be preferred.

Effective Kotlin Item 55: Consider associating elements to a map

How associateBy is useful to improve the performance of finding elements.

Effective Kotlin Item 56: Consider using groupingBy instead of groupBy

What Grouping is, and how groupingBy instead of groupBy can be a performance optimization.

Effective Kotlin Item 60: Use appropriate collection types

Using non-standard collection types to improve performance in Kotlin.

Updates

Book update and public chapters

25 April 2021

We start a process of updating the book, and sharing some chapters online as public articles.

New items

14 October 2019

Generics are important for effective Kotlin development, and yet many experienced developers have trouble using them. That's is why we just introduced three new items dedicated to generics in Kotlin:

Item 22: Use generics when implementing common algorithms Item 23: Avoid shadowing type parameters Item 24: Consider variance for generic types

Beta version available!

26 August 2019

The book is finally published! It took a lot of time and energy, but Effective Kotlin is finally available in e-book.

Alpha testing

26 July 2019

Alpha testing started today - reviewers are reading the beta version of the book and giving final suggestions. It means that Effective Kotlin should be published within two months.