← Back to context

Comment by BrandoElFollito

4 years ago

Disclamer: amateur developer for 25 years, no formal education in that area

a loop that iterates over indices when I want elements is not readable, e.g. I prefer

    for element in elements:

rather than

    for (i = 0 , i < len(elements), i++) { element = elements[i] ...

This is maybe where this aversion comes from, people usually [citation needed] want to iterate over elements, rather than indices.

I find that many times in more complex loops you need the index as well. Sometimes for as mundane reason as logging.

  • Yes, my code is not that complicated and I use languages that are rather high level (Python, Golang, JS with Vue) so I needed the index I think once when I had to remove an element from an array in JS and for some reason I was not using lodash.

    But yes, there are of course cases where the index could be needed, I was merely commenting on the aversion part for generic developers.