| I'm not against using for loops when what you need is an actual loop.
The thing is most of the times, previously, for loops where actually doing something for which there are concepts that express exactly what was being done - though not in all languages. For instance, map - I know that it will return a new collection of exactly the same number of items the iterable being iterated has. When used correctly it shouldn't produce any side-effects outside the mapping of each element. In some languages now you have for x in y which in my opinion is quite ok as well, but still to change the collection it has to mutate it, and it's not immediate what it will do. If I see a reduce I know it will iterate again a definite number of times, and that it will return something else than the original iterable (usually), reducing a given collection into something else. On the other hand forEach should tell me that we're only interested in side-effects. When these things are used with their semantic context in mind, it becomes slightly easier to grasp immediately what is the scope of what they're doing. On the other hand, with a for (especially the common, old school one) loop you really never know. I also don't understand what is complex about the functional counterparts -
for (initialise_var, condition, post/pre action) can only be simpler in my mind due to familiarity as it can have a lot of small nuances that impact how the iteration goes - although to be honest, most of the times it isn't complex either - but does seem slightly more complex and with less contextual information about the intent behind the code. |