Leanpub Header

Skip to main content

Effective Kotlin

Best practices

Effective Kotlin summarizes the best practices and experiences of the Kotlin community, together with a deep explanation of some lesser-known Kotlin functionalities. All of the best practices are presented as simple rules with detailed explanations.

The author is letting you choose the price you pay for this book!

Pick Your Price...
PDF
EPUB
WEB
3,767
Readers
385
Pages
About

About

About the Book

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.

If you are interested in paperback, you can purchase it here.

Price

Pick Your Price...

Minimum price

$25.00

$29.90

You pay

$29.90

Author earns

$23.92
$

All prices are in US $. You can pay in US $ or in your local currency when you check out.

EU customers: prices exclude VAT, which is added during checkout.

...Or Buy With Credits!

Number of credits (Minimum 2)

2
The author will earn $24.00 from your purchase!
You can get credits monthly with a Reader Membership

Author

About the Author

Marcin Moskała

Marcin Moskala is an experienced developer and Kotlin trainer. He is the founder of the kt.academy, Kotlin GDE, an official JetBrains partner for teaching Kotlin, and author of the books Effective Kotlin, Kotlin Coroutines, and Android Development with Kotlin.

Leanpub Podcast

Episode 254

An Interview with Marcin Moskała

Contents

Table of Contents

Introduction: Be pragmatic

  1. Who is this book for?
  2. Code Sources

Part 1: Good code

Chapter 1: Safety

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

Chapter 2: Readability

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

Part 2: Code design

Chapter 3: Reusability

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

Chapter 4: Abstraction design

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

Chapter 5: Object creation

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

Chapter 6: Class design

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

Part 3: Efficiency

Chapter 7: Make it cheap

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

Chapter 8: Efficient collection processing

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

Dictionary

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub