1# 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 * @param param1 ... 26 * @param param2 ... 27 * @param 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](/docs/api_guidelines/index.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### 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 182### Don't use angle brackets for `@param` 183 184Instead of: 185 186```kotlin {.bad} 187/** 188 * @param <T> my cool param 189 */ 190``` 191 192Do this: 193 194```kotlin {.good} 195/** 196 * @param T my cool param 197 */ 198``` 199 200This syntax is correct in Javadoc, but angle brackets aren't used in KDoc 201([@param reference guide](https://kotlinlang.org/docs/kotlin-doc.html#param-name)). 202 203## Javadoc - KDoc differences 204 205Some tags are shared between Javadoc and KDoc, such as `@param`, but there are 206notable differences between the syntax and tags supported. Unsupported syntax / 207tags do not currently show as an error in the IDE / during the build, so be 208careful to look out for the following important changes. 209 210### Hiding documentation 211 212Using `@suppress` will stop documentation being generated for a particular 213element. This is equivalent to using `@hide` in Android Javadoc. 214 215### Deprecation 216 217To mark an element as deprecated, use the `@Deprecated` annotation on the 218corresponding declaration, and consider including a `ReplaceWith` fragment to 219suggest the replacement for deprecated APIs. 220 221```kotlin {.good} 222package androidx.somepackage 223 224@Deprecated( 225 "Renamed to Bar", 226 replaceWith = ReplaceWith( 227 expression = "Bar", 228 // import(s) to be added 229 "androidx.somepackage.Bar" 230 ) 231) 232class Foo 233 234class Bar 235``` 236 237This is equivalent to using the `@deprecated` tag in Javadoc, but allows 238specifying more detailed deprecation messages, and different 'severity' levels 239of deprecation. For more information see the documentation for 240[@Deprecated](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deprecated/). 241 242### Linking to elements 243 244To link to another element, put its name in square brackets. For example, to 245refer to the class `Foo`, use `[Foo]`. This is equivalent to `{@link Foo}` in 246Javadoc. You can also use a custom label, similar to Markdown: `[this 247class][Foo]`. 248 249### Code spans 250 251To mark some text as code, surround the text with a backtick (\`) as in 252Markdown. For example, \`true\`. This is equivalent to `{@code true}` in 253Javadoc. 254 255### Inline markup 256 257KDoc uses Markdown for inline markup, as opposed to Javadoc which uses HTML. The 258IDE / build will not show a warning if you use HTML tags such as `<p>`, so be 259careful not to accidentally use these in KDoc. 260