Which is better? ```// 1 val numAdded = api.addNo...
# codingconventions
e
Which is better?
Copy code
// 1
val numAdded = api.addNote(
    modelId,
    deckId,
    fields,
    tags = setOf(crn)
)
return numAdded == 1
Copy code
// 2
return api.addNote(
    modelId,
    deckId,
    fields,
    tags = setOf(crn)
).let {
    it == 1
}
Copy code
// 3
return api.addNote(modelId, deckId, fields, tags = setOf(crn)) == 1
3️⃣ 5
1️⃣ 29
j
Very strongly leaning towards option 1. It's usually not very readable to mix expressions and side effects IMO. And the return value's meaning is not trivial, so naming the variable helps a lot
🙏 1
c
I prefer 1, but I wouldn't mind 3 either. I think 2 is confusing.
🙏 1
o
might prefer 3 depending on what the function is named and what else is in it, otherwise 1
🙏 1
z
def not 2
p
Both 2 and 3 obscure the fact the return value is dependent on the condition as it has some distance from the
return
keyword. I'd also query whether the condition should be
>= 1
assuming the function is intended to return a boolean indicating whether a note was added or not. Should it ever add more than one it would currently return
false
. The formatting difference between 1 and 3 makes 3 seem much more succinct than 1 but there's really not much in it when formatting similarly:
Copy code
// 1
val numAdded = api.addNote(modelId, deckId, fields, tags = setOf(crn))
return numAdded == 1
vs
Copy code
// 3
return api.addNote(modelId, deckId, fields, tags = setOf(crn)) == 1
You could also flip the condition to bring it closer to the return and therefore more obvious on a cursory read.
Copy code
// 3b
return 1 == api.addNote(modelId, deckId, fields, tags = setOf(crn))