Categories
Programming Swift

Do-while-false?

Have you already use Do-While-False in some project ?

This is of course not much applicable in Swift as we have guard let, but in C++, Java or ObjectiveC, this could be very useful in some cases.

Let me past an example I found in old project.

The purpose of the function here doesn’t really matter. What’s mattered for us at that time was:

  • We exclude all possible error cases, easily identifiable.
  • We have one single exit point from the function, at the end. Adding new condition in the function was straight forward
  • Avoid if-cascade
  • In case, the function was returning void, we were applying the same principle, one exit point, better for future modifications.
  • do while, was service only the purpose of having control flow, and not actually looping anything.

Today, with guard let, we can’t have one exit point, as the compiler forces us to return the function immediately.

What do you think about this syntax? Would you use this kind of code in your project?

Thank you for reading until here…  👊 Have a good week end…  🍺