https://kotlinlang.org logo
Title
e

Ellen Spertus

08/20/2022, 5:58 PM
I'm excited that I'll be teaching Intro CS with Kotlin this fall. Could someone review my use of KDoc in this code? I know my use of values is unidiomatic.
/**
 * Prompts a user for their name and returns their response.
 */
fun getUserName(): String {
    println("What is your name? ")
    val newName = readln()
    return newName
}

/**
 * Prints a greeting, using the person's [name].
 */
fun greetUser(name: String) {
    println("Hello, $name")
}

/**
 * Carries on a brief conversation with a user.
 */
fun converse() {
    val name = getUserName()
    greetUser(name)
}
t

Tom Yuval

08/20/2022, 6:16 PM
KDoc looks good to me 👍 What do you mean by “use of values” (which is supposedly unidiomatic)? Is it the fact that various values are put in `val`s only to be used once immediately afterwards? Small nitpick: there’s a space at the end of the printed string in
getUserName
, which doesn’t seem to need to be there, given as that it is `println`; I guess the intention was to use
print
? (If not, I’d remove the space.)
e

Ellen Spertus

08/20/2022, 6:32 PM
Thanks a lot, @Tom Yuval.
What do you mean by “use of values” (which is supposedly unidiomatic)? Is it the fact that various values are put in `val`s only to be used once immediately afterwards?
Yes.
Small nitpick: there’s a space at the end of the printed string in
getUserName
, which doesn’t seem to need to be there, given as that it is `println`;
Thank you. I'm having them do things from the repl, which didn't do what I wanted when the space wasn't present, but I see it doesn't do what I want even when the space is present. 😄
getUserName()
What is your name?Ellen
res0: kotlin.String = Ellen
p

Phani Mahesh

08/23/2022, 10:05 AM
Having vals that are immediately used is fine if the variable name provides useful context to the reader. I would even recommend it if used judiciously.