https://kotlinlang.org logo
Title
c

Colton Idle

03/16/2022, 2:40 AM
This method works nicely if a person has a middle name, but if they don't. Then it print
[FIRST]..[LAST]
Notice the two dots are supposed to show that there are two spaces. What's a convenient way of only having a single space?
private fun Person.prettyPrint() =
        "${this.data.firstName} ${this.data.middleName} ${this.data.lastName}"
a

Adam Powell

03/16/2022, 2:50 AM
use a collection and
.joinToString
, but also https://shinesolutions.com/2018/01/08/falsehoods-programmers-believe-about-names-with-examples/ 🙂
☝️ 2
☝️🏻 1
c

Colton Idle

03/16/2022, 4:08 AM
Yeah. I have that website favorited. In this case, the data model and everything is already created and I'm just trying to move this bug fix along. I did let them know that we could/should probably just keep everything as a single string.
r

Richard Gomez

03/16/2022, 1:32 PM
For this type of thing you could also just write the different formats explicitly in a conditional statement. For example:
data class Component(val type: String, val namespace: String? = null, val name: String, val version: String) {
    val displayName: String by lazy { if (namespace != null) "$namespace:$name" else name }
}
e

ephemient

03/16/2022, 4:13 PM
as a convenience, if some parts may be null and you want to skip them,
listOfNotNull(first, middle, last).joinToString(" ")
(or
.takeIf { it.isNotEmpty() }
to make empty strings into null so that
listOfNotNull
skips them) but yeah, human names definitely don't all fit that model
👍 2