-
Notifications
You must be signed in to change notification settings - Fork 14
for vs forEach #33
Comments
Use Prefer |
I personally like using
I don't see a problem with |
Good point. Function references obviously lend themselves very well to being used with |
The function is more flexible, for sure. I'd also be OK with just standardising on "use forEach everywhere". It's more the consistency issue that's a pain. |
These two are equivalent; the `Collections.forEach` method [is an inline `for`][]. So let's pick one. [is an inline `for`]: https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/common/src/generated/_Collections.kt#L1814-L1820 I prefer the method syntax since it composes with other methods more clearly, provides for autocomplete functionality, and promotes an "objects with behavior"-first thinking (in opposition to C, Java, Rust, etc., that promotes an imperative mindset). Some people have [found `.forEach` to be faster][] when [used on a collection][], somehow, though with worse performance on integer range loops. [found `.forEach` to be faster]: https://medium.com/mobile-app-development-publication/kotlin-for-loop-vs-foreach-7eb594960333 [used on a collection]: https://blog.gouline.net/kotlin-bits-for-loops-vs-foreach-30548d7472a5 It's worth noting that the Kotlin style guide has had [an open issue][] on this topic for four years, with people also leaning towards `.forEach` everywhere. [an open issue]: Kotlin/kotlin-style-guide#33
These two are equivalent; the `Collections.forEach` method [is an inline `for`][]. So let's pick one. [is an inline `for`]: https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/common/src/generated/_Collections.kt#L1814-L1820 I prefer the method syntax since it composes with other methods more clearly, provides for autocomplete functionality, and promotes an "objects with behavior"-first thinking (in opposition to C, Java, Rust, etc., that promotes an imperative mindset). Some people have [found `.forEach` to be faster][] when [used on a collection][], somehow, though with worse performance on integer range loops. [found `.forEach` to be faster]: https://medium.com/mobile-app-development-publication/kotlin-for-loop-vs-foreach-7eb594960333 [used on a collection]: https://blog.gouline.net/kotlin-bits-for-loops-vs-foreach-30548d7472a5 It's worth noting that the Kotlin style guide has had [an open issue][] on this topic for four years, with people also leaning towards `.forEach` everywhere. [an open issue]: Kotlin/kotlin-style-guide#33
I think Item 46 and also Item 45 from Effective Java (3rd Edition) may be relevant to this discussion. |
Based on the readme, guidance on
I recommend removing it from the coding conventions until a consensus has been reached. |
For loops vs forEach - two very similar constructs with very similar syntaxes:
for (foo in foos) { foo.thing() }
vs
foos.forEach { it.thing() }
I prefer the traditional for form, seeing as that's what forEach becomes anyway and it encourages you to pick a more meaningful iterator name than it, but I'm happy with either.
Suggested rule: use
forEach
only as the terminal operation of a chain of functional operations (map, filter, groupBy, etc). Don't use a variable name followed by.forEach { }
instead of a for loop.The text was updated successfully, but these errors were encountered: