Introduction: Why Kotlin Documentation Requires a Special Approach
\nKotlin, being a modern multi-paradigm language, offers developers unique features: null-safety, extension functions, coroutines, and DSL. However, these same features create challenges when documenting. Ordinary JavaDoc-style comments are often unable to adequately describe complex Kotlin constructs. At an advanced level, it is not enough to simply write a "function description." It is necessary to create documentation that is useful both for internal teams and for external library users.
\nIn this article, we will look at professional tools and techniques for writing documentation for Kotlin projects: from using KDoc with advanced tags to configuring Dokka and integrating automatic site generation.
\n\n1. KDoc: Advanced Features and Tags
\nKDoc is the standard markup language for Kotlin, based on JavaDoc, but with extensions for the specifics of the language. Advanced developers know that KDoc supports Markdown inside comments, which makes it more powerful than JavaDoc.
\n\n1.1. Basic KDoc Tags
\n- \n
@param— description of a function or class parameter. \n @return— description of the return value. \n @throws/@exception— description of exceptions. \n @see— link to another code element. \n @sample— embedding a code example from a separate file. \n @property— documenting class properties. \n @receiver— documenting an extension function (describing whatthisrepresents). \n
1.2. Example of Advanced KDoc Usage
\nConsider an example of an extension function with coroutines and null-safety:
\n\n/**\n * Asynchronously transforms a list of [items] using the given [transform] function.\n *\n * This function uses coroutines for parallel processing of elements.\n * Unlike the standard [map], it performs transformations concurrently.\n *\n * @param T the type of elements in the source list.\n * @param R the type of elements in the resulting list.\n * @param items the list of elements to transform.\n * @param transform a suspend function applied to each element.\n * @return a list of transformed elements.\n * @throws CancellationException if the coroutine was cancelled during execution.\n * @sample com.example.ParallelMapExample.main\n * @receiver not applicable (top-level function).\n */\nsuspend fun <T, R> parallelMap(\n items: List<T>,\n transform: suspend (T) -> R\n): List<R> {\n return coroutineScope {\n items.map { async { transform(it) } }.awaitAll()\n }\n}\n\nNote the @sample tag. It allows you to reference a real file with an example, which will be automatically included in the documentation when generated via Dokka.
1.3. Documenting DSL
\nFor DSL (Domain-Specific Languages) in Kotlin, it is especially important to document the context. Use the @receiver tag to specify which object is the receiver in the lambda:
/**\n * Creates an HTML table.\n *\n * @param init the initializer lambda in the context of [TableBuilder].\n * @receiver [TableBuilder] — the object providing methods for configuring the table.\n */\nfun htmlTable(init: TableBuilder.() -> Unit): String {\n val builder = TableBuilder()\n builder.init()\n return builder.render()\n}\n\n2. Dokka: Configuring Documentation Generation for Your Project
\nDokka is the official tool for generating documentation from KDoc. At an advanced level, it is important to configure it for specific needs: multi-module projects, integration with external libraries, and customization of the appearance.
\n\n2.1. Basic Gradle Configuration
\nAdd the Dokka plugin