Ellen Spertus
01/09/2021, 12:34 AMrollButton.text = "Press"
, as shown by the underscore under `text`:pawegio
02/03/2021, 3:44 PMEllen Spertus
02/17/2021, 9:26 PMboxes.forEach {
it.setOnClickListener {
makeColored(it)
}
}
2️⃣
boxes.forEach { it.setOnClickListener { makeColored(it) } }
3️⃣ @Marc Knaup
boxes.forEach { it.setOnClickListener(::makeColored) }
4️⃣ @ephemient
for (box in boxes) {
box.setOnClickListener { makeColored(it) }
}
5️⃣ @Marc Knaup @ephemient
for (box in boxes) {
box.setOnClickListener(::makeColored)
}
arekolek
03/04/2021, 1:57 PMlistOf(1, 2, 3).map { Unit }
IDE marks Unit
as redundant and proposes to replace it with:
listOf(1, 2, 3).map { }
How do you handle this? Proposed fix looks weird to me. This issue occurs quite often in RxJava streamsdiego-gomez-olvera
03/05/2021, 1:33 PMShawn
03/05/2021, 4:10 PMval foo = Foo(
bar,
baz,
qux
)
“so readable” “looks right” “as god intended”
val foo = Foo(
bar,
baz,
qux,
)
“ew gross” “_literally_ unreadable” “`parse error: Expected another key-value pair`”miqbaldc
04/02/2021, 5:50 AMdata class State(title: String, description: String)
fun State.toSessionExpired(): State {
val state = this
return with(state) {
// when token expired
if (httpCode == 401) {
copy(
title = getString(R.string.error_expired_title),
description = getString(R.string.error_expired_description),
)
} else this
}
}
therealbluepandabear
04/09/2021, 5:40 AMval notificationHelper: INotificationHelper = NotificationHelper(this)
val notificationHelperTwo = NotificationHelper(this)
therealbluepandabear
04/11/2021, 11:36 PMstartActivityForResult(
CheatActivity.newIntent(this@MainActivity, quizViewModel.currentQuestionAnswer),
REQUEST_CODE_CHEAT,
ActivityOptions.makeCustomAnimation(
this,
R.anim.fragment_fade_enter,
R.anim.fragment_fade_exit
).toBundle()
)
val intent = CheatActivity.newIntent(this@MainActivity, quizViewModel.currentQuestionAnswer)
val animation = ActivityOptions.makeCustomAnimation(this, R.anim.fragment_fade_enter, R.anim.fragment_fade_exit).toBundle()
startActivityForResult(intent, REQUEST_CODE_CHEAT, animation)
Mark
04/12/2021, 2:27 AMAny?
or Unit
for the return type? I guess Any?
is the appropriate type, but I wonder if it adds confusion for example in private functions where you know the return type of the lambda is always Unit
.
1. Any?
always
2. Unit
always
3. Any?
when public, Unit
when private and possibleMatteo Mirk
04/20/2021, 3:00 PMTomasz Krakowiak
04/21/2021, 4:59 PMmarcinmoskala
04/29/2021, 5:42 AMEllen Spertus
05/04/2021, 5:27 PMif (b == true)
2️⃣
if (b == true) // b could be null
3️⃣
if (b != null && b)
christophsturm
05/05/2021, 2:29 PMarekolek
05/10/2021, 7:20 PMfun foo(bar: Bar) {
if (!bar.canFoo) {
<http://logger.info|logger.info>("No foo for this bar")
return
}
// Make foo with bar
}
2️⃣
fun foo(bar: Bar) {
if (!bar.canFoo) {
return <http://logger.info|logger.info>("No foo for this bar")
}
// Make foo with bar
}
Mark
05/13/2021, 2:40 PMfun interface
as a verb when the function is invoke
? Or maybe as a noun like here:
fun interface ItemSelector {
operator fun invoke(items: List<String>): String
}
val selectItem = object : ItemSelector { ... }
val itemSelected = selectItem( ... )
1. Verb
2. Nounmarcinmoskala
05/14/2021, 8:17 AMSam
05/23/2021, 7:19 AMdp
to px
Solution 1:
fun Int.dpToPx(context: Context): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), context.resources.displayMetrics).toInt()
}
Solution 2:
fun Int.dpToPx() = (Resources.getSystem().displayMetrics.density * this + 0.5f)
so Which once is correct for multiple screen? Thanks.Marc Knaup
06/04/2021, 3:05 PMfun interface SomeLongTypeNameFactory {
fun create(): SomeLongTypeName
}
Con: Causes conflict if a class implements multiple factories.
2️⃣ Function name explicitly mentions type
fun interface SomeLongTypeNameFactory {
fun createSomeLongTypeName(): SomeLongTypeName
}
Con: Long & repetitive.
3️⃣ Other convention (add comment)Chris
06/09/2021, 4:50 PMit.toString()
or
2️⃣ "$it"
or
3️⃣ something else?
Is 1️⃣ and 2️⃣ equivalent?Victor Cardona
06/10/2021, 11:54 AMclass A {
fun doSomething(): Unit {
...
}
private val B.things: List<Thing>
get() = ...
private fun C.helpMe(): Thing { ... }
}
I currently have the extensions at the top-level, but from reading the guidance it sounds like I should move them into the class itself.miqbaldc
06/17/2021, 10:59 PMprivate fun AccountInfo?.isNotEmpty() = this != null &&
id.isNullOrBlank().not() &&
status.isNullOrBlank().not() &&
something.isNullOrBlank().not()
2️⃣
private fun AccountInfo?.isNotEmpty() = this != null
&& id.isNullOrBlank().not()
&& status.isNullOrBlank().not()
&& something.isNullOrBlank().not()
3️⃣ (linter will complain when exceeded the max length)
private fun AccountInfo?.isNotEmpty() = this != null && id.isNullOrBlank().not() && status.isNullOrBlank().not() && something.isNullOrBlank().not()
miqbaldc
06/18/2021, 6:28 AMfun doSomething(): String {
return if
aMethodHere1()
else
aMethodHere2()
}
2️⃣
fun doSomething(): String {
return if aMethodHere1()
else aMethodHere2()
}
3️⃣
fun doSomething(): String {
return if aMethodHere1() else aMethodHere2()
}
Mark
06/22/2021, 3:35 AMclass Foo {
companion object {
private fun bar1() { ... }
}
}
private fun bar2() { ... }
private fun Foo.Companion.bar3() { ... }
1️⃣ companion object
2️⃣ top-level (file)
3️⃣ top-level companion extensionmiqbaldc
07/12/2021, 8:09 AM// `cache` is an interface that has method `newToken()` & `unrecoverableSession()`
Which one do you guys prefer, on the following snippet code?
1️⃣
if (retryCount > RETRY_LIMIT) {
cache.unrecoverableSession()
return null
}
cache.newToken()?.let { newToken ->
return rewriteRequest(newToken)
}
return null
or
2️⃣
if (retryCount > RETRY_LIMIT) {
cache.unrecoverableSession()
return null
}
val newToken = cache.newToken()
return if (newToken != null) {
rewriteRequest(newToken)
} else {
cache.unrecoverableSession()
null
}
or
3️⃣
val newToken = cache.newToken()
return when {
retryCount > RETRY_LIMIT -> {
cache.unrecoverableSession()
null
}
newToken != null -> rewriteRequest(newToken)
else -> {
cache.unrecoverableSession()
null
}
}
KV
07/15/2021, 9:17 AM@Serializable
data class ApiResponse<T>(
@SerialName("data") val data: DataResponse<T>,
@SerialName("meta") val meta: MetaResponse?,
@SerialName("included") val included: List<IncludedResponse>? = null
)
Class 2 has - List<DataResponse>, MetaResponse, List<IncludedResponse>
@Serializable
data class ListApiResponse<T>(
@SerialName("data") val data: List<DataResponse<T>>,
@SerialName("meta") val meta: MetaResponse?,
@SerialName("included") val included: List<IncludedResponse>? = null
)
In both the class List<IncludedResponse>?
& MetaResponse
both are common
only uncommon thing is DataResponse
& List<DataResponse>
.miqbaldc
07/30/2021, 3:23 PMUserV1
to UserV2
from our shared prefs
Which one do you prefer?
1️⃣
UserV1
& UserV2
is nullable if fetched from server, but we provide a blank/default value
data class UserV1(status: String? = "", phoneNumber: String? = "", areaCode: Int? = -1)
data class UserV2(
accountInfo: Account? = null,
domicileInfo: Domicile? = null,
)
data class Account(status: String? = "", phoneNumber: String? = "")
data class Domicile(areaCode: Int? = -1)
fun UserV1?.toUserV2() = UserV2(
accountInfo = Account(status = this?.status.orEmpty(), phoneNumber = this?.phoneNumber.orEmpty()),
domicileInfo = Domicile(areaCode = this?.areaCode ?: -1)
)
fun UserV2?.orEmpty() = UserV2()
// wrapping the whole if condition, e.g: (if a else b).orEmpty()
fun findUser() = (if (userV2?.status?.isNullOrBlank() == false) userV2 else userV1.toUserV2()).orEmpty()
2️⃣
UserV1
& UserV2
is nullable if fetched from server, and leave it null (no blank/default value)
data class UserV1(status: String? = null, phoneNumber: String? = null, areaCode: Int? = null)
data class UserV2(
accountInfo: Account? = null,
domicileInfo: Domicile? = null,
)
data class Account(status: String? = null, phoneNumber: String? = null)
data class Domicile(areaCode: Int? = null)
fun UserV1?.toUserV2() = UserV2(
accountInfo = Account(status = this?.status.orEmpty(), phoneNumber = this?.phoneNumber.orEmpty()),
domicileInfo = Domicile(areaCode = this?.areaCode ?: -1)
)
fun UserV2?.orEmpty() = UserV2()
// call the `.orEmpty()` twice, e.g: if a.orEmpty() else b.orEmpty()
fun findUser() = if (userV2?.status?.isNullOrBlank() == false) userV2.orEmpty() else userV1.toUserV2().orEmpty()
Dennis Titze
08/12/2021, 11:51 AMJames Whitehead
08/12/2021, 1:56 PMmutableSetOf()
and linkedSetOf()
.
Given that both methods return a LinkedHashSet
is there a recommendation on when to use one vs the other?James Whitehead
08/12/2021, 1:56 PMmutableSetOf()
and linkedSetOf()
.
Given that both methods return a LinkedHashSet
is there a recommendation on when to use one vs the other?diesieben07
08/12/2021, 2:09 PMmutableSetOf
does not guarantee that you get a LinkedHashSet
. All it guarantees is that you get a mutable set that preserves iteration order. linkedHashSet
will always return a LinkedHashSet
.
So personally I'd always go for mutableSetOf
unless you specifically need a LinkedHashSet
.