Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Comments and Printing Data  ›  Lesson

Comments

Comments 12 minKotlin has line comments with //, block comments with /* */ that can nest, and KDoc comments with /** */ that document declarations; good comments explain why, not what.

Comments

Six months from now, someone opens the café code and finds if (cups >= 10) price = 0.0. Is that a bug or a promotion? One well-placed sentence would have told them. Comments are notes the compiler ignores and people rely on.

The rule

Code says what; comments say why.

Kotlin has three kinds of comment:

// A line comment runs to the end of the line.

/* A block comment
   can span several lines. */

/**
 * A KDoc comment documents the declaration below it.
 * @param cups drinks bought so far, including this one
 * @return 0.0 for a free drink, otherwise the menu price
 */
fun loyaltyPrice(cups: Int, price: Double): Double =
    if (cups % 10 == 0) 0.0 else price   // every tenth drink is free
  • // is for short notes, including at the end of a line of code.
  • /* … */ covers several lines. Unlike Java, Kotlin block comments nest: a /* … */ inside another one is fine, which makes it safe to comment out code that already contains a block comment.
  • /** … */ is KDoc, Kotlin's documentation format. It sits directly above a class, function or property. Tags such as @param, @return and @throws describe the parts, and it supports Markdown like **bold** and [references] to other names. IDEs show KDoc when you hover, and tools such as Dokka turn it into a documentation website.

What makes a comment good? It survives changes to the code. // add 1 to cups goes stale the moment someone edits the line. // counts the free drink too, so the tenth cup triggers the offer stays true and explains a decision you could not guess from the code.

The shortcut

Before writing a comment, cover the code and read only the comment. If it tells you nothing the code would not, delete it or rewrite it as a reason. If the code is confusing, first try a better name; comment only what a name cannot say.

Improving one commentStep 1 of 4
  1. The original

    val t = p * 1.05 // multiply p by 1.05

A common trap is leaving large blocks of old code commented out "just in case". Version control remembers old code; comments should not.

Drills

Answer fast — trust the rule.

quick drill

Which comment adds the most value?

quick drill

Which opener starts a KDoc documentation comment?

quick drill

What happens with /* outer /* inner */ still outer */ in Kotlin?

quick drill

In KDoc, which tag describes a function's parameter?

Remember

  • Code says what; comments say why.
  • // for a line, /* */ for a block (and it nests), /** */ for KDoc.
  • Cover the code: if the comment says nothing new, delete or rewrite it.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainA comment worth keeping

The café code contains if (cups >= 10) price = 0.0 // set price to zero. Explain what is weak about this comment, rewrite it so it helps a future reader, and add a KDoc comment for a function fun loyaltyPrice(cups: Int, price: Double): Double.