When you write any loop, imagine that someday, for some reason, this loop will iterate over 100x the amount of times you anticipated it would. Does it break? Do things go down? Run out of memory? Time out?

Eventually, at 2am, it will 100x. Best be ready.

3
Share
Share on Mastodon
Share on Twitter
Share on Facebook
Share on Linkedin
Ivan Moscoso

@nateberkopec such a great question. Caveat that I’ve seen some devs get hung up on whether this implies a kind of scope creep and/or gold-plating exercise than the “ok look, just ponder on it, just do that one thing right now” thought exercise it should start as.

1
1y
Damax

@nateberkopec I recently changed a loop with 25k object straight from the database. Now it’s Rails find_each that do 1k per batch. Still need to load 25k entries for export 🥲

0
1y
Felipe

@nateberkopec in the cases where I expect the loop to be few iterations, I add a counter and throw if it goes over some limit.

0
1y
Replies