Chapter 9: Idioms and Anti-Patterns
In the previous chapters, we discussed the different aspects of the Kotlin programming language, the benefits of functional programming, and concurrent design patterns.
This chapter discusses the best and worst practices in Kotlin. You'll learn what idiomatic Kotlin code should look like and which patterns to avoid. This chapter contains a collection of best practices spanning those different topics.
In this chapter, we will cover the following topics:
- Using the scope functions
- Type checks and casts
- An alternative to the try-with-resources statement
- Inline functions
- Implementing algebraic data types
- Reified generics
- Using constants efficiently
- Constructor overload
- Dealing with nulls
- Making asynchronicity explicit
- Validating input
- Preferring sealed classes over enums
After completing this chapter, you should be able to write more readable and maintainable Kotlin code, as well as...