Effective Kotlin: Enums and Annotations

Let’s see how we can apply Joshua Bloch’s Effective Java in the Kotlin world. Today’s topic is Enums and Annotations.

Item 34: Use enums instead of int constants

There is nothing special in Kotlin for this item. However, for Android, we can use annotations like IntDef to get certain benefits, such as compile-time type safety.

Item 35: Use instance fields instead of ordinals

There is nothing special in Kotlin for this item.

Item 36: Use EnumSet instead of bit fields

There is nothing special in Kotlin for this item.

Item 37: Use EnumMap instead of ordinal indexing

There is nothing special in Kotlin for this item.

Item 38: Emulate extensible enums with interfaces

In Kotlin, you can also consider using sealed classes, which is more flexible than enums, while still restricting class hierarchies.

Item 39: Prefer annotations to naming patterns

There is nothing special in Kotlin for this item.

Item 40: Consistently use the Override annotation

In Kotlin, we have to use the override modifier when overriding methods.

Item 41: Use marker interfaces to define types

There is nothing special in Kotlin for this item.


See also

comments powered by Disqus