This is something I would have expected to read 20 years ago, but now? ForEach has been around a long time and not having to use a loop index feels normal and has since.

And I don’t like for-loops anymore. When I do use them I split them across three lines but most of the time now I use while with a loop index initialized before the loop and and incremented at the bottom of it. The classical C style for-loop violates my strict one-operation-per-line rule and I’ve abandoned it along with the ! operator and continue keyword.

Edit: reactions to some of the other comments

  • If you’ve been programming more than a year or two and you’re still using returns anywhere but at the end of a function then you should consider another line of work. This is really basic stuff. Some consider it a controversial matter. Not that it can’t be done by careful and experienced people but it leads to an impulsive “just get out” kind of thinking that shows up in all kinds of sloppy practices. I’ve used early returns three times in 25 years.
  • Performance matters in some code, usually about 1% of it. In the great majority of code we write stability is paramount and performance is a tertiary concern because we’re implementing a UI or using data from a network call that took about a trillion times as long as any time saved by the difference between for and foreach. Since we have container classes that are meant to be iterated, foreach makes perfect sense. Grip < — get one

American Software Developer living in Vietnam. Classical musician (guitar, woodwinds), weightlifter, multilingual, misanthrope • XY

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store