blob: c53ba8c2d1df798169c27b7b2b6a3165851f223d [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
120## Javadoc - KDoc differences
121
122Some tags are shared between Javadoc and KDoc, such as `@param`, but there are
123notable differences between the syntax and tags supported. Unsupported syntax /
124tags do not currently show as an error in the IDE / during the build, so be
125careful to look out for the following important changes.
126
127### Hiding documentation
128
129Using `@suppress` will stop documentation being generated for a particular
130element. This is equivalent to using `@hide` in Android Javadoc.
131
132### Deprecation
133
134To mark an element as deprecated, use the `@Deprecated` annotation on the
135corresponding declaration, and consider including a `ReplaceWith` fragment to
136suggest the replacement for deprecated APIs.
137
138```kotlin {.good}
139package androidx.somepackage
140
141@Deprecated(
142 "Renamed to Bar",
143 replaceWith = ReplaceWith(
144 expression = "Bar",
145 // import(s) to be added
146 "androidx.somepackage.Bar"
147 )
148)
149class Foo
150
151class Bar
152```
153
154This is equivalent to using the `@deprecated` tag in Javadoc, but allows
155specifying more detailed deprecation messages, and different 'severity' levels
156of deprecation. For more information see the documentation for
157[@Deprecated](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deprecated/).
158
159### Linking to elements
160
161To link to another element, put its name in square brackets. For example, to
162refer to the class `Foo`, use `[Foo]`. This is equivalent to `{@link Foo}` in
163Javadoc. You can also use a custom label, similar to Markdown: `[this
164class][Foo]`.
165
166### Code spans
167
168To mark some text as code, surround the text with a backtick (\`) as in
169Markdown. For example, \`true\`. This is equivalent to `{@code true}` in
170Javadoc.
171
172### Inline markup
173
174KDoc uses Markdown for inline markup, as opposed to Javadoc which uses HTML. The
175IDE / build will not show a warning if you use HTML tags such as `<p>`, so be
176careful not to accidentally use these in KDoc.