Skip to content

Commit 0c2cdd3

Browse files
samharpsarahhaggarty
authored andcommitted
update: clarify cases in coding conventions
Adjust grammar usage when describing uppercase, lowercase, camelCase, Pascal case, and other style cases; replace word "case" with synonyms when not describing one of the cases above (e.g. "example", "circumstance", etc.)
1 parent 582727c commit 0c2cdd3

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed

docs/topics/coding-conventions.md

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ files in `org.example.kotlin.network.socket` should be in the `network/socket` s
4141
If a Kotlin file contains a single class or interface (potentially with related top-level declarations), its name should be the same
4242
as the name of the class, with the `.kt` extension appended. It applies to all types of classes and interfaces.
4343
If a file contains multiple classes, or only top-level declarations, choose a name describing what the file contains, and name the file accordingly.
44-
Use [an upper camel case](https://en.wikipedia.org/wiki/Camel_case) with an uppercase first letter (also known as Pascal case),
45-
for example, `ProcessDeclarations.kt`.
44+
Use [upper camel case](https://en.wikipedia.org/wiki/Camel_case), where the first letter of each word is capitalized.
45+
For example, `ProcessDeclarations.kt`.
4646

4747
The name of the file should describe what the code in the file does. Therefore, you should avoid using meaningless
4848
words such as `Util` in file names.
@@ -81,7 +81,7 @@ have FQN `myPackage.PlatformKt`. This produces the "Duplicate JVM classes" error
8181
The simplest way to avoid that is renaming one of the files according to the guideline above. This naming scheme helps
8282
avoid clashes while retaining code readability.
8383

84-
> There are two cases when these recommendations may seem redundant, but we still advise to follow them:
84+
> There are two scenarios where these recommendations may seem redundant, but we still advise to follow them:
8585
>
8686
> * Non-JVM platforms don't have issues with duplicating file facades. However, this naming scheme can help you keep
8787
> file naming consistent.
@@ -135,9 +135,9 @@ Package and class naming rules in Kotlin are quite simple:
135135

136136
* Names of packages are always lowercase and do not use underscores (`org.example.project`). Using multi-word
137137
names is generally discouraged, but if you do need to use multiple words, you can either just concatenate them together
138-
or use the camel case (`org.example.myProject`).
138+
or use camel case (`org.example.myProject`).
139139

140-
* Names of classes and objects start with an uppercase letter and use the camel case:
140+
* Names of classes and objects use upper camel case:
141141

142142
```kotlin
143143
open class DeclarationProcessor { /*...*/ }
@@ -147,7 +147,7 @@ object EmptyDeclarationProcessor : DeclarationProcessor() { /*...*/ }
147147

148148
### Function names
149149

150-
Names of functions, properties and local variables start with a lowercase letter and use the camel case and no underscores:
150+
Names of functions, properties and local variables start with a lowercase letter and use camel case with no underscores:
151151

152152
```kotlin
153153
fun processDeclarations() { /*...*/ }
@@ -181,8 +181,8 @@ class MyTestCase {
181181
### Property names
182182

183183
Names of constants (properties marked with `const`, or top-level or object `val` properties with no custom `get` function
184-
that hold deeply immutable data) should use uppercase underscore-separated ([screaming snake case](https://en.wikipedia.org/wiki/Snake_case))
185-
names:
184+
that hold deeply immutable data) should use all uppercase, underscore-separated names following the ([screaming snake case](https://en.wikipedia.org/wiki/Snake_case))
185+
convention:
186186

187187
```kotlin
188188
const val MAX_COUNT = 8
@@ -201,7 +201,7 @@ Names of properties holding references to singleton objects can use the same nam
201201
val PersonComparator: Comparator<Person> = /*...*/
202202
```
203203

204-
For enum constants, it's OK to use either uppercase underscore-separated names ([screaming snake case](https://en.wikipedia.org/wiki/Snake_case))
204+
For enum constants, it's OK to use either all uppercase, underscore-separated ([screaming snake case](https://en.wikipedia.org/wiki/Snake_case)) names
205205
(`enum class Color { RED, GREEN }`) or upper camel case names, depending on the usage.
206206

207207
### Names for backing properties
@@ -257,11 +257,8 @@ if (elements != null) {
257257
### Horizontal whitespace
258258

259259
* Put spaces around binary operators (`a + b`). Exception: don't put spaces around the "range to" operator (`0..i`).
260-
261260
* Do not put spaces around unary operators (`a++`).
262-
263261
* Put spaces between control flow keywords (`if`, `when`, `for`, and `while`) and the corresponding opening parenthesis.
264-
265262
* Do not put a space before an opening parenthesis in a primary constructor declaration, method declaration or method call.
266263

267264
```kotlin
@@ -274,28 +271,23 @@ fun bar() {
274271
}
275272
```
276273

277-
* Never put a space after `(`, `[`, or before `]`, `)`
278-
279-
* Never put a space around `.` or `?.`: `foo.bar().filter { it > 2 }.joinToString()`, `foo?.bar()`
280-
281-
* Put a space after `//`: `// This is a comment`
282-
283-
* Do not put spaces around angle brackets used to specify type parameters: `class Map<K, V> { ... }`
284-
285-
* Do not put spaces around `::`: `Foo::class`, `String::length`
286-
287-
* Do not put a space before `?` used to mark a nullable type: `String?`
274+
* Never put a space after `(`, `[`, or before `]`, `)`.
275+
* Never put a space around `.` or `?.`: `foo.bar().filter { it > 2 }.joinToString()`, `foo?.bar()`.
276+
* Put a space after `//`: `// This is a comment`.
277+
* Do not put spaces around angle brackets used to specify type parameters: `class Map<K, V> { ... }`.
278+
* Do not put spaces around `::`: `Foo::class`, `String::length`.
279+
* Do not put a space before `?` used to mark a nullable type: `String?`.
288280

289281
As a general rule, avoid horizontal alignment of any kind. Renaming an identifier to a name with a different length
290282
should not affect the formatting of either the declaration or any of the usages.
291283

292284
### Colon
293285

294-
Put a space before `:` in the following cases:
286+
Put a space before `:` in the following scenarios:
295287

296-
* when it's used to separate a type and a supertype
297-
* when delegating to a superclass constructor or a different constructor of the same class
298-
* after the `object` keyword
288+
* When it's used to separate a type and a supertype.
289+
* When delegating to a superclass constructor or a different constructor of the same class.
290+
* After the `object` keyword.
299291

300292
Don't put a space before `:` when it separates a declaration and its type.
301293

@@ -1062,14 +1054,14 @@ Learn the difference between [Java and Kotlin multiline strings](java-to-kotlin-
10621054

10631055
### Functions vs properties
10641056

1065-
In some cases, functions with no arguments might be interchangeable with read-only properties.
1057+
In some scenarios, functions with no arguments might be interchangeable with read-only properties.
10661058
Although the semantics are similar, there are some stylistic conventions on when to prefer one to another.
10671059

10681060
Prefer a property over a function when the underlying algorithm:
10691061

1070-
* does not throw
1071-
* is cheap to calculate (or cached on the first run)
1072-
* returns the same result over invocations if the object state hasn't changed
1062+
* Does not throw.
1063+
* Is cheap to calculate (or cached on the first run).
1064+
* Returns the same result over invocations if the object state hasn't changed.
10731065

10741066
### Extension functions
10751067

@@ -1137,10 +1129,10 @@ For the guidance on choosing the right scope function for your case, refer to [S
11371129

11381130
When writing libraries, it's recommended to follow an additional set of rules to ensure API stability:
11391131

1140-
* Always explicitly specify member visibility (to avoid accidentally exposing declarations as public API)
1132+
* Always explicitly specify member visibility (to avoid accidentally exposing declarations as public API).
11411133
* Always explicitly specify function return types and property types (to avoid accidentally changing the return type
1142-
when the implementation changes)
1134+
when the implementation changes).
11431135
* Provide [KDoc](kotlin-doc.md) comments for all public members, except for overrides that do not require any new documentation
1144-
(to support generating documentation for the library)
1136+
(to support generating documentation for the library).
11451137

11461138
Learn more about best practices and ideas to consider when writing an API for your library in the [Library authors' guidelines](api-guidelines-introduction.md).

0 commit comments

Comments
 (0)