style guide says I should use classic for loop over forEach. I am paranoid android and dont want to instantiate iterators on arraylists, so, would creating a extension function forXYZ, which is just a for until loop, be considered antipattern?
k
karelpeeters
10/20/2018, 7:37 AM
You're afraid of creating
Iterators
? Why?
d
Dominaezzz
10/20/2018, 9:57 AM
Although iterators are fairly lightweight. They are optmized out and an indexed for loop is used under the hood. (For arrays only). 🙂
k
karelpeeters
10/20/2018, 10:01 AM
For arrays, sure. But for lists? Anyway, the difference really shouldn't matter.
u
ursus
10/21/2018, 2:17 PM
Well looking at the bytecode the iterator is there. No im not afraid, but if there is a solution without it, with same level of sugar, why not? Can be helpful on android
k
karelpeeters
10/21/2018, 3:05 PM
You would love
C++
I think. It's big on those "costless abstractions", the JVM ecosystem not so much.
u
ursus
10/21/2018, 3:54 PM
Well, android endorsed it, and given list are usually small, i think its reasonable to do so, and its theoretically faster. And with kotlin you can give it the same amount of sugar