The naming table
| Identifier | Convention | Example |
|---|---|---|
| Packages | lowercase, no underscores | org.example.project |
| Classes and objects | UpperCamelCase | DeclarationProcessor |
| Functions | camelCase starting lowercase | processDeclarations() |
| Properties and local variables | camelCase starting lowercase | declarationCount |
| Constants | SCREAMING_SNAKE_CASE | MAX_COUNT |
| Backing properties | underscore prefix | _elementList |
| Files with one class | the class name plus .kt | DeclarationProcessor.kt |
What counts as a constant
SCREAMING_SNAKE_CASE is for const val properties and for top-level or object val properties with no custom getter that hold deeply immutable data. A top-level val that holds a mutable collection or an object with behavior stays camelCase. A property that refers to a singleton object can use the object naming style, as in val PersonComparator. Enum constants may be either SCREAMING_SNAKE_CASE or UpperCamelCase.
Acronyms: two letters vs three or more
A two-letter acronym keeps both capitals: IOStream. A longer one capitalizes only its first letter: XmlFormatter, HttpInputStream.
Functions that look like classes
Two kinds of functions are named like classes. A factory function may share the name of the abstract type it returns, as in fun Foo(): Foo. And @Composable functions that return Unit use UpperCamelCase, as in TabHeader.
Test names may contain spaces
In tests only, you can name a method with backticks and spaces, as in fun `ensure everything works`(), or use underscores. Kotlin notes that names with spaces are only supported on Android from API level 30.
Package and file names
Multi-word package names are discouraged; if you need one, join the words or use camelCase, as in org.example.myProject. A file holding several declarations gets a descriptive UpperCamelCase name, such as ProcessDeclarations.kt; avoid meaningless names like Util.
Converting names with Transform Case
Our snake_case, camelCase, PascalCase and CONSTANT_CASE converters treat an acronym as one word, so HTTP server error becomes HttpServerError and http_server_error. For three-letter and longer acronyms that matches Kotlin. For a two-letter acronym, put both capitals back: IoStream becomes IOStream.
Naming conventions in other languages: Python, Java, C#, Go (Golang), Rust, Swift, Dart, PHP. All side by side: 20 languages compared.
Sources
- Kotlin coding conventions: Naming rules, JetBrains / kotlinlang.org
Questions
Is it XMLParser or XmlParser in Kotlin?XmlParser. Kotlin capitalizes only the first letter of acronyms longer than two letters; two-letter acronyms keep both capitals, as in IOStream.
When does a Kotlin val use SCREAMING_SNAKE_CASE?
When it's a const val, or a top-level or object val with no custom getter holding deeply immutable data.
Can Kotlin test names have spaces?
Yes, in backticks, but only in tests. On Android this needs API level 30 or later.