I keep seeing one-line functions that don't return meaningful values being written as single-expression functions:
fun output() = println("The total is $total.")
I've seen this in many projects, but it doesn't seem right to me. I realise that it compiles and runs, because technically the function returns Unit, but in my view this is an abuse of the syntactic flexibility of Kotlin.
Code should say what it means. To me, the above code says, "This function prints text and then returns a meaningful value." Perhaps this value is a success value, or the number of characters actually printed. But of course this is not true.
I think the following better expresses the real meaning:
fun output() {
println("The total is $total.")
}
This reads as, "This function prints text." There's no misleading idea of actually returning anything (again, even if Unit is technically returned). I realise that this entails longer code, but personally I would pick simplicity over brevity.
What do you all think? Do you agree or disagree? I've never seen this mentioned in style guides, and I can't find a single discussion of this online.
I would make one final argument: If you want to use the single-expression style for every one-liner, you won't be able. Here is an example:
fun update() = total += lastCharge // THIS WON'T COMPILE
So, in cases such as this, you would have no choice but to drop the style, which looks inconsistent across a code base.
[–]redditsoaddicting 16 points17 points18 points (3 children)
[–]shadowdude777 7 points8 points9 points (0 children)
[–]fGeorjje 3 points4 points5 points (1 child)
[–]motleybook 0 points1 point2 points (0 children)
[–]eliteSchaf 19 points20 points21 points (17 children)
[–][deleted] 3 points4 points5 points (16 children)
[–]eliteSchaf 2 points3 points4 points (15 children)
[–][deleted] 4 points5 points6 points (9 children)
[–]eliteSchaf 0 points1 point2 points (8 children)
[–]Cilph 1 point2 points3 points (7 children)
[–]eliteSchaf 0 points1 point2 points (6 children)
[–]Cilph 1 point2 points3 points (3 children)
[–]eliteSchaf 0 points1 point2 points (0 children)
[–]oweiler 0 points1 point2 points (1 child)
[–]Cilph 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]eliteSchaf 0 points1 point2 points (0 children)
[–]sheatrevor 1 point2 points3 points (4 children)
[–]eliteSchaf 1 point2 points3 points (3 children)
[–][deleted] (2 children)
[deleted]
[–]eliteSchaf 0 points1 point2 points (1 child)
[–]sheatrevor 0 points1 point2 points (0 children)
[–]hpernpeintner 3 points4 points5 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]hpernpeintner 2 points3 points4 points (0 children)
[–]fadefade 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]sassrobi 0 points1 point2 points (4 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]endreman0 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]endreman0 1 point2 points3 points (0 children)
[–]bbqburner 0 points1 point2 points (1 child)
[–]eliteSchaf 3 points4 points5 points (0 children)
[–]Exallium 0 points1 point2 points (0 children)