Comments and Documentation

Now that we are starting to assemble Kotlin classes, it is time to address the two things that developers seem to dislike most: testing and documentation.

How you write tests for your Kotlin code will depend a lot on the environment in which you are running that code. So, for example, Android app developers using Kotlin will still write JUnit tests, just using Kotlin instead of Java. As a result, there is little that a book on the Kotlin language can tell you about testing Kotlin, which is why you will see little on this topic here.

However, when it comes to documenting your Kotlin code, Kotlin supports code comments, much like most programming languages. And, just as Java has JavaDocs and Ruby has RDoc and so forth, Kotlin has KDoc for creating comments that can be turned into documentation.

Basic Comment Syntax

Kotlin adopted Java comment syntax, which happens to line up with JavaScript comment syntax.

// indicates that the rest of the line is a comment:

val foo = 0     // this is a comment
// so is this

/* and */ are used to delimit multiple lines as being a comment:

val foo = 0     // this line has executable code

/*
val bar = 0     // this line does not, as it is wrapped in a comment
*/

The biggest — and perhaps only — difference between Kotlin comments and what you might do in Java is that multi-line comments can be nested in Kotlin. So, for example, this is valid in Kotlin:

/*
  It's
  /*
    comments
    /*
      all
      /*
        the
        /*
          way
          /*
            down!
          */
        */
      */
    */
  */
*/

Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.