Effective Kotlin: Item 22 — Use interfaces only to define types

In item 22 of Joshua Bloch’s famous book Effective Java he concludes that interfaces should only be used to define types and not to merely export constants. The following example shows the constant interface anti-pattern, i.e. what not to do:

public interface PhysicsConstants {
static final double ELECTRON_MASS = 9.109_383_56e-31;
}

In Java there are two approaches, use an enum type (item 34) or a non-instantiable utility class (item 4). Using Kotlin’s Object declarations is one way to create such a class:

object PhysicsConstants {
const val ELECTRON_MASS = 9.109_383_56e-31
}

The const modifier marks the property as a compile-time constant which improves performance by giving direct access without a getter along with the possibility of being inlined.

An alternative to creating an object, as above, is to use top-level declarations where possible. For better Java compatibility you should use the @file:JvmName annotation, so the constants don’t appear in a file postfixed with “Kt”.

@file:JvmName("PhysicsConstants")

const val ELECTRON_MASS = 9.109_383_56e-31

However, as Dmitry Jemerov from JetBrains says in Best practices for top-level declarations:

The recommended practice is to never use object for creating namespaces, and to always use top-level declarations when possible. We haven’t found name conflicts to be an issue, and if you do get a conflict, you can resolve it using an import with alias.

While I agree about the use of top-level declarations for extension functions I’m still not entirely convinced about their use for constants — perhaps I like namespaces but that being said realistically in a single code base how often do you have two constants in different classes with the same name. What are your thoughts?

For a more in-depth dive into constants on Kotlin I highly recommend reading Where Should I Keep My Constants in Kotlin?

Each week I am looking at “items” from Joshua Bloch’s well-respected book, Effective Java to see how it applies to Kotlin. You can find the rest of the items I’ve covered at Effective Kotlin. Please let me know your thoughts.

Matt Dolan has been eating doughnuts and developing with Android since the dark days of v1.6.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store