Do you agree with a convention of using `it` only ...
# codingconventions
e
Do you agree with a convention of using
it
only in single-line lambdas? Example Good:
urls.filter { it.startsWith("https://") }
Bad (the further down code goes, the harder to understand what
it
means):
Copy code
urls.forEach {
  // some code doing some complex stuff 
  // some more code to increase cognitive load 
  val response = request(it)
  <http://logger.info|logger.info>("Request to $it got response: $response")
}
🚫 3
👌 1
k
I don't agree that
it
should be used only in single-line lambdas, but at the same time I disagree with using it in lambdas with too many lines (the more lines, the more I disagree with using
it
). It's not black/white, but shades of grey. In most cases, up to 3 lines is ok. In particular, I avoid using
it
in any lambda that has another lambda nested inside it.
3
🙏 2
e
I think it depends; I'd be ok with a simple nest
Copy code
lines.filter { it.all { it.isUpperCase() } }
because it's still clear what each name refers to. but if it's not clear then definitely name them
2
t
it
can be used in any lambda expression, and I don't see any reason why you couldn't use it for more than one line; although it should be strictly avoided if you are nesting lambda expressions within each other, mostly because of the confusion.