blob: ef33b04f23579371896cb3db20fdf64c9ce76690 [file] [log] [blame] [view]
AndroidX Core Team03b4da32021-03-10 23:20:41 +00001# Kotlin documentation (KDoc) guidelines
2
3[TOC]
4
5This guide contains documentation guidance specific to Jetpack Kotlin APIs.
6General guidance from
7s.android.com/api-guidelines#docs
8should still be followed, this guide contains extra guidelines specifically for
9Kotlin documentation. For detailed information on KDoc's supported tags and
10syntax, see the
11[official documentation](https://kotlinlang.org/docs/kotlin-doc.html).
12
13## Guidelines for writing KDoc
14
15### Every parameter / property in a function / class should be documented
16
17Without an explicit `@param` / `@property` tag, a table entry for a particular
18parameter / property will not be generated. Make sure to add tags for *every*
19element to generate full documentation for an API.
20
21```kotlin {.good}
22/**
23 * ...
24 *
25 * @param1 ...
26 * @param2 ...
27 * @param3 ...
28 */
29fun foo(param1: Int, param2: String, param3: Boolean) {}
30```
31
32### Consider using all available tags to add documentation for elements
33
34Kotlin allows defining public APIs in a concise manner, which means that it can
35be easy to forget to write documentation for a specific element. For example,
36consider the following contrived example:
37
38```kotlin
39class Item<T>(val label: String, content: T)
40
41fun <T> Item<T>.appendContent(content: T): Item<T> { ... }
42```
43
44The class declaration contains:
45
46* A generic type - `T`
47* A property (that is also a constructor parameter) - `label`
48* A constructor parameter - `content`
49* A constructor function - `Item(label, content)`
50
51The function declaration contains:
52
53* A generic type - `T`
54* A receiver - `Item<T>`
55* A parameter - `content`
56* A return type - `Item<T>`
57
58When writing KDoc, consider adding documentation for each element:
59
60```kotlin {.good}
61/**
62 * An Item represents content inside a list...
63 *
64 * @param T the type of the content for this Item
65 * @property label optional label for this Item
66 * @param content the content for this Item
67 * @constructor creates a new Item
68 */
69class Item<T>(val label: String? = null, content: T)
70
71/**
72 * Appends [content] to [this] [Item], returning a new [Item].
73 *
74 * @param T the type of the content in this [Item]
75 * @receiver the [Item] to append [content] to
76 * @param content the [content] that will be appended to [this]
77 * @return a new [Item] representing [this] with [content] appended
78 */
79fun <T> Item<T>.appendContent(content: T): Item<T> { ... }
80```
81
82You may omit documentation for simple cases, such as a constructor for a data
83class that just sets properties and has no side effects, but in general it can
84be helpful to add documentation for all parts of an API.
85
86### Use `@sample` for each API that represents a standalone feature, or advanced behavior for a feature
87
88`@sample` allows you to reference a Kotlin function as sample code inside
89documentation. The body of the function will be added to the generated
90documentation inside a code block, allowing you to show usage for a particular
91API. Since this function is real Kotlin code that will be compiled and linted
92during the build, the sample will always be up to date with the API, reducing
93maintenance. You can use multiple samples per KDoc, with text in between
94explaining what the samples are showing. For more information on using
95`@sample`, see the
96[API guidelines](/company/teams/androidx/api_guidelines.md#sample-code-in-kotlin-modules).
97
98### Do not link to the same identifier inside documentation
99
100Avoid creating self-referential links:
101
102```kotlin {.bad}
103/**
104 * [Item] is ...
105 */
106class Item
107```
108
109These links are not actionable, as they will take the user to the same element
110they are already reading - instead refer to the element in the matching case
111without a link:
112
113```kotlin {.good}
114/**
115 * Item is ...
116 */
117class Item
118```
119
AndroidX Core Teamee9c1aa2021-04-06 17:29:05 +0000120### Include class name in `@see`
121
122When referring to a function using `@see`, include the class name with the
123function - even if the function the `see` is referring to is in the same class.
124
125Instead of:
126
127```kotlin {.bad}
128/**
129 * @see .myCoolFun
130 */
131```
132
133Do this:
134
135```kotlin {.good}
136/**
137 * @see MyCoolClass.myCoolFun
138 */
139```
140
141### Match `@param` with usage
142
143When using `@param` to refer to a variable, the spelling must match the
144variable's code declaration.
145
146Instead of:
147
148```kotlin {.bad}
149/**
150 * @param myParam
151 */
152public var myParameter: String
153```
154
155Do this:
156
157```kotlin {.good}
158/**
159 * @param myParameter
160 */
161public var myParameter: String
162```
163
164### Don't mix `@see` and Markdown links
165
166Instead of:
167
168```kotlin {.bad}
169/**
170 * @see [MyCoolClass.myCoolFun()][androidx.library.myCoolFun] for more details.
171 */
172```
173
174Do this:
175
176```kotlin {.good}
177/**
178 * See [MyCoolClass.myCoolFun()][androidx.library.myCoolFun] for more details.
179 */
180```
181
AndroidX Core Team03b4da32021-03-10 23:20:41 +0000182## Javadoc - KDoc differences
183
184Some tags are shared between Javadoc and KDoc, such as `@param`, but there are
185notable differences between the syntax and tags supported. Unsupported syntax /
186tags do not currently show as an error in the IDE / during the build, so be
187careful to look out for the following important changes.
188
189### Hiding documentation
190
191Using `@suppress` will stop documentation being generated for a particular
192element. This is equivalent to using `@hide` in Android Javadoc.
193
194### Deprecation
195
196To mark an element as deprecated, use the `@Deprecated` annotation on the
197corresponding declaration, and consider including a `ReplaceWith` fragment to
198suggest the replacement for deprecated APIs.
199
200```kotlin {.good}
201package androidx.somepackage
202
203@Deprecated(
204 "Renamed to Bar",
205 replaceWith = ReplaceWith(
206 expression = "Bar",
207 // import(s) to be added
208 "androidx.somepackage.Bar"
209 )
210)
211class Foo
212
213class Bar
214```
215
216This is equivalent to using the `@deprecated` tag in Javadoc, but allows
217specifying more detailed deprecation messages, and different 'severity' levels
218of deprecation. For more information see the documentation for
219[@Deprecated](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deprecated/).
220
221### Linking to elements
222
223To link to another element, put its name in square brackets. For example, to
224refer to the class `Foo`, use `[Foo]`. This is equivalent to `{@link Foo}` in
225Javadoc. You can also use a custom label, similar to Markdown: `[this
226class][Foo]`.
227
228### Code spans
229
230To mark some text as code, surround the text with a backtick (\`) as in
231Markdown. For example, \`true\`. This is equivalent to `{@code true}` in
232Javadoc.
233
234### Inline markup
235
236KDoc uses Markdown for inline markup, as opposed to Javadoc which uses HTML. The
237IDE / build will not show a warning if you use HTML tags such as `<p>`, so be
238careful not to accidentally use these in KDoc.