Hello all, Quick formatting question, when chainin...
# announcements
a
Hello all, Quick formatting question, when chaining calls with lambdas, which one is correct: (provided the code inside the brackets is longer than one line) 1.
Copy code
(1..10).filter {
    it < 5
}
    .map {
        val double = it * 2
        double*3
    }
    .map {
        it.dec()
        it + 5
    }
2.
Copy code
(1..10).filter {
    it < 5
}.map {
    val double = it * 2
    double * 3
}.map {
    it.dec()
    it + 5
}
The coding convention suggests that when chaining calls you should put each call on a new line starting with a dot but I don’t how if it applies to lambdas and other {} things
1️⃣ 1
2️⃣ 6
d
Whatever rocks your boat 🙂
1
d
2. is easier to read and I don't think this rule applys here, but that is only my oppinion
f
Copy code
(1..10).filter { it < 5 }
    .map {
        val double = it * 2
        double * 3
    }
    .map {
        it.dec()
        it + 5
    }
is what I would go for
1
d
I like @flosch’s version but when chaining sequence operations like this, in the real world, maybe extract functions with descriptive names for the maps
a
@David Eriksson Agree about the extraction for more complicated mappings, I just picked the map function here to give a simplified example! The thing with 2 is that if you alternate single line and multi-line lambdas, your code looks weird alternating between }.map and .map calls
👍 1
s
Since this all depends on things like “are there more than one line inside the lambda”, I find it a little hard to have conventions for these things, so I generally agree with the “whatever rocks your goat” answer. I believe in this case, I would do it like @flosch above, but perhaps move the
.filter
down:
Copy code
(1..10)
    .filter { it < 5 }
    .map {
        val double = it * 2
        double*3
    }
    .map {
        it.dec()
        it + 5
    }
d
"First make it work, then make it beautiful" 🙂
👍 1
s
First make it work, then make a test, then make it beautiful. 😏
d
Or... first make a test that doesn't work... etc 😉
🔥 1
c
For lambdas with multiple very short statements, I might even use a
;
to separate them so they can stay on a single line. I try to make the call chains each take only a single line, either by condensing into one line or extracting to a named function
Copy code
(1..10)
    .filter { it < 5 }
    .map { val double = it * 2; double*3 }
    .map { it.dec(); it + 5 }
no red 1
👌 1
a
I do think you lose readability doing that, I’d rather have more lines in this case (or as you said extract it)
c
It all depends on what you’re trying to make apparent. Is it more important to quickly the exact transformations, or just an understanding of what transformations are being done (the overall data flow)
a
In my case it’s about understanding more complicated data flows, so readability is the main objective
a
Copy code
(1..10)
    .filter { it < 5 }
    .map {
        val double = it * 2
        double * 3
    }
    .map {
        it.dec()
        it + 5
    }
I would go with this variant