Does anyone have any thoughts about always putting...
# codingconventions
b
Does anyone have any thoughts about always putting a space before a lambda? It seems to me like there should be no space as to make it similar to function call syntax?
Copy code
// Why is it that we are encouraged to add spaces before EVERY lambda?
// Seems to me like the spaces arbitrarily break up this expression.
list.filter { it < 10 }.map { it * it }

// This is the long-form syntax, right?
list.filter({ it < 10 }).map({ it * it })

// So after a little syntactic sugar, we should end up like this?
list.filter{ it < 10 }.map{ it * it }
t
interesting point. I'd say having the space makes it closer to a function declaration
b
Yeah, that's true. That's what I thought at first, but they are sorta fundamentally different, right? A function declaration is, to some degree, a special statement (it can have different modifiers and such). Whereas, a lambda is a self-contained expression that I think should be treated as just regular data.
k
I don't think I have a space there
m
do you mean lambda or last function parameter that's also a lambda? Actually doesn't matter, I like both the way they are. Function name immediately followed by
{
just seems weird to me
b
I agree with the function declaration case. I mean, any function parameter that is a lambda. So you don't feel that the extra spaces in the
list.filter
example are obtrusive?
m
not at all. This just feels like the
{
is part of the
filter
word, and the rest just stands there alone
Copy code
list.filter{ it < 10 }
  .map{ it * it }
b
Mmmmm, ok. Interesting take. Maybe after a while I'll get used to it. atm I just see that as a good thing, like I want to see that tight cohesion between
{
and
filter
because it's like a direct argument of it. (I guess it was only just a few weeks ago that I disliked the primary constructor being slapped in the middle of a class declaration, but now I've come to enjoy it.)
m
what's your main programming language btw? before kotlin of course 🙂
b
A lot of C back in the day. Then had a job in Python for a while. Now been doing Java Android dev for a few years and just getting into Kotlin now. So, a lot of different things. 😉
🙂 1
e
For a person used to writing
class Foo { … }
,
synchonized { … }
,
do { … } while
, etc it is very natural to put a space before an opening curly brace.
☝️ 2
b
Ok, it makes sense to keep everything consistent. I guess the
list.filter
case is just a minor edge case relative to the most common uses of curly braces.
t
you must be new to kotlin 😉
b
Yes indeed.
Still learning the ropes.
t
I just mean you'll realize soon that higher order functions are no edge case at all in kotlin
b
I just meant the case where you might have two lambdas on a single line like that, relative to all others. But yeah, it's great that after decades of Java's Object Land™, we finally have first class functions.