Best-known use case: break out of deeply nested for/while blocks.
- Alternative — extract into a function and use early return instead of goto. If you need “goto: CleanUp”, then you can extract the cleanup block into a function returning the same data type and replace the goto with “return cleanup()”
- Alternative — extract the cleanup block into a void function and replace the goto with a call to cleanup()
- Alternative (easiest) — exit or throw exception
Sometimes none of the alternatives are easy. To refactor the code to avoid goto requires too much testing, approval and release. The code is in a critical module in production. Laser surgery is preferred — introduce goto.