This book is a guide for Kotlin developers on how to become an excellent Kotlin developer. Kotlin is a powerful and pragmatic language, but it's not enough to know about its features. You also need to know when they should be used and in what way. This book 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. It's a comprehensive guide of best practices for Kotlin code quality: safety, readability, code design and efficiency. What you will learn You will learn how to make better Kotlin development in terms of safety, readability, maintainability and performance. The book also covers some advanced topics like inline functions and classes, DSLs or platform types. Who this book is for This book was written for Kotlin developers who want to learn and understand how to write high-quality code. It assumes some experience with Kotlin or at least with Swift, Java or Scala. It is directed towards all kinds of Kotlin developers and isn't specific to mobile or backend development. The purpose of this book The purpose of Effective Kotlin is to teach and promote the best practices for Kotlin developers, as well as the way Kotlin features can and should be used to improve code. Reader Testimonials Rafal Kuźmiński: Well done! I've read a few items and I have to admit that I learned some interesting stuff. Great book: ) Hanno Günther: Really love this book!
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
【One-Line Pitch】
A rule-by-rule field guide for developers who already know Kotlin syntax but want to write code that is safer, clearer, and easier to maintain. Read it if you want judgment about *when* to use a feature, not another tour of the language.
【Book Arc】
- **Opening (~0%–10%)**: Frames the book's pragmatic philosophy and opens the Safety chapter by arguing that mutability is the root of many bugs, then shows how Kotlin's `val`, read-only collections, and related features help you limit it.
- **Early (~10%–35%)**: Continues the safety toolkit — synchronization and concurrent collections for shared state, tightening variable scope, declaring expectations with `require`/`check`/`error`, handling nullability and `lateinit`, choosing between `null`, `Result`, and exceptions, and writing unit tests.
- **Middle (~35%–50%)**: Shifts to Readability and Reusability: reducing cognitive load, avoiding clever-but-obscure constructs like misused `let`, operator overloading discipline, receiver/DSL design, and when code repetition is actually the right call versus when to extract abstractions.
- **Late (~50%–85%)**: Moves into code design and efficiency — the excerpts do not cover these chapters in detail, but the blurb and introduction promise advanced material on inline functions and classes, DSLs, and platform types.
- **Ending (~85%–100%)**: The excerpts do not cover the closing chapters; expect the book to consolidate its practices rather than introduce new themes.
【Book Arc note】
The excerpt sample is front-loaded: roughly the first half of the book (Safety, Readability, early Reusability) is well represented, while design, efficiency, and advanced topics are only signaled by the blurb and introduction.
【Key Takeaways】
- **Mutability is a cost, not a default** (Opening): The book opens by enumerating why mutable state is hard to reason about, test, and parallelize, then recommends mutability only where it earns its keep — Kotlin's `val` and read-only types make limiting it cheap.
- **Protect shared state deliberately** (Early): Synchronized blocks with a lock, atomic values, and concurrent collections are presented as tools with trade-offs; the `UserRepository` example shows synchronizing only writes when reads dominate.
- **Declare expectations as early as possible** (Early): `require` for arguments, `check` for state, `error` for impossible states, and the Elvis operator with `return`/`throw` turn implicit assumptions into explicit, fail-fast code.
- **Match error style to expectation** (Early): Return `null` or `Result.failure` for *expected* errors; throw exceptions for *unexpected* ones. `Result` wins over nullable when you need to carry error details like codes or messages.
- **Tighten scope to shrink state** (Early): Prefer local variables over properties and declare variables in the narrowest block that uses them — fewer visible mutation points means fewer states to reason about and test.
- **Optimize for cognitive load, not cleverness** (Middle): Familiar structures beat short ones; the misused-`let` example shows how a popular idiom can silently change behavior. The book explicitly warns against over-correcting into banning idioms outright.
- **Repetition is not always duplication** (Middle): Extract only when two pieces of code represent the *same knowledge* — ask whether they are more likely to change together. Independent build configs that merely look alike should stay separate.
- **Type inference can leak into APIs** (Middle): The `DEFAULT_CAR` example shows how inferring a public value's type can silently narrow an interface's contract; specify types at API boundaries.
【Reading Tips】
- **Read Safety and Readability closely; skim what you already practice.** These chapters are dense with concrete before/after snippets and are the book's strongest material per the excerpts.
- **Treat each item as a checklist, not a lecture.** The rule-of-thumb format rewards jumping to items relevant to code you're writing now; the summaries at chapter ends are good refreshers.
- **Pause on the counterexamples.** The `let` misuse, `DEFAULT_CAR` inference, and premature-extraction cases teach judgment better than the positive rules do — note *why* the "bad" version fails.
- **Expect the advanced chapters (inline, DSL, platform types) to be harder.** The excerpts don't cover them, so budget extra time and have the companion source repository open.
- **Take away one habit per chapter.** E.g., from Safety: default to `val` and narrow scope; from Readability: prefer explicit receivers in DSLs.
【Coverage Limits】
This guide is based on stratified excerpts concentrated in the book's first half (Safety, Readability, early Reusability); the design, efficiency, and advanced-topic chapters are described only via the blurb and introduction, so their specific items are not summarized here.
Page 8
d at a university by a scientist, Martin Odersky. He wanted to transfer some concepts from functional programming into the Java object- oriented programmingw...
current modifications, Java also provides classes to repre- sent atomic values and concurrent collections. • There are also libraries that providemultiplatfo...
on, gives us correct results for the first 5 numbers: @Test fun `fib works correctly for the first 5 positions`() { assertEquals(1, fib(0)) assertEquals(1, f...
xtract them. But what if we do that? These two applications are independent, so if we need to change something in the configuration, we will most likely need...
: (List<User>)->Unit) { } An example from the stdlib: @Deprecated("Use readBytes() overload without "+ "estimatedSize parameter", ReplaceWith("readBytes()"))...
tem 34: Consider defining a DSL for complex object creation A set of Kotlin features used together allows us to make a configuration-like Domain Specific Lan...
ed classes and interfaces to express restricted hierarchies Sealed classes and interfaces represent restricted hierarchies, whichmeans hier- archies with a c...
kage. This fact is usedwhenwe cannot add amember ourselves. It is also used in projects designed to separate data and behavior. Properties with fields need t...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Loading comments...
Reply to Comment
Edit Comment