do { ... } while (0);
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:
1 | $category = 'someCategory'; |
Yuck. An obvious solution would have been to refactor this logic into a method and return the data if it’s found, avoiding the extra checks. If this were JavaScript, a function expression would have done the job:
1 | var result = (() => { |
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:
1 | do { |
The 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.