I’m currently buried in over 20” of snow, so I might as well post something.
I wrote a piece of PHP code a while back to grab some data from a cache; if it couldn’t find what it needed, it should try a couple of other scenarios before giving up. The code looked something like the following:
But this was PHP. I’m guessing at the time I didn’t want to bother making a new method for a one-off operation, so I left it as is. Maybe. There are many times I look at my old code and wonder what the hell I was thinking.
Months later, a veteran on my team refactored the file. When I checked the code again, I saw this block in its place:
do-while loop will only execute once and creates a separate block that can be broken out of at any time. It was a pretty neat trick for avoiding another method that I hadn’t seen before.