This method works nicely if a person has a middle ...
# codereview
c
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?
Copy code
private fun Person.prettyPrint() =
        "${this.data.firstName} ${this.data.middleName} ${this.data.lastName}"
a
☝️ 2
☝🏻 1
c
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
For this type of thing you could also just write the different formats explicitly in a conditional statement. For example:
Copy code
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
as a convenience, if some parts may be null and you want to skip them,
Copy code
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