8000 for vs forEach · Issue #33 · Kotlin/kotlin-style-guide · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

for vs forEach #33

Open
mikehearn opened this issue Jan 5, 2017 · 6 comments
Open

for vs forEach #33

mikehearn opened this issue Jan 5, 2017 · 6 comments

Comments

@mikehearn
Copy link

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.

@cypressious
Copy link
cypressious commented Jan 5, 2017

Use collection?.forEach {} if the collection is nullable.

Prefer for over forEach if you need to use break. As continue can be emulated by return@forEach, using it in either is fine.

@voddan
Copy link
voddan commented Jan 5, 2017

I personally like using forEach if the operation I need to perform is short, like printing the element:

names.forEach {println(it)}
//or
names.forEach(::println)

I don't see a problem with forEach unless people try to do break or continue and invent "cleaver" workarounds

@cypressious
Copy link

Good point. Function references obviously lend themselves very well to being used with forEach.

@mikehearn
Copy link
Author

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.

mike-burns added a commit to thoughtbot/guides that referenced this issue Nov 20, 2020
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
mike-burns added a commit to thoughtbot/guides that referenced this issue Dec 4, 2020
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
@mahozad
Copy link
mahozad commented Oct 3, 2021

I think Item 46 and also Item 45 from Effective Java (3rd Edition) may be relevant to this discussion.

@cable729
Copy link
cable729 commented Feb 3, 2023

Based on the readme, guidance on for vs forEach should not have been moved to the official coding conventions reference:

Issues where a sufficient amount of consensus has been reached are closed, and the rules are moved to the official style guide at https://kotlinlang.org/docs/reference/coding-conventions.html.

I recommend removing it from the coding conventions until a consensus has been reached.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
0