Why avoid gotos
It is easy to get caught in an infinite loop if the goto point is above the goto call. This is almost guaranteed if there is no reliable escape from the code, such as a RETURN statement within a conditional statement. It might be tempting to put a goto statement within a catch block. If there is an error, catch it, and then jump to a certain section of code that performs some recovery action, right?
The problem with this is that if you don't cancel your catch and an error occurs later down the road after the goto point, it will be re-caught by the catch statement, and then you have the infinite loop problem again hint: always cancel your catch before performing recovery action.
If you do cancel the catch, however, then you've just cancelled the handler for your mainline code, and then you jump right back into it. Additional errors will not be handled. Unless you have a different catch block somewhere in the routine, or maybe one somewhere at a higher scope level.
When that happens, good luck debugging see "Confusing Code" above. A catch block should usually perform a recovery and then return out of the routine rather than try to jump somewhere else in the routine. In addition to the examples above, there are a handful of scenarios where using GOTO might sound like the best or easiest thing to do. Syntax of goto statement is:. Here in the goto the name1 mentioned identify the place where the branch is to be made.
The name1 is a valid variable name followed by a colon. The name1 is placed immediately before the statement where the control is to be transformed. Using a goto statement throws that assumption out the window, and all of the sudden, the game is uncertain. It's a new and uncertain rule. Yeah, goto's seem fine to YOU if you wrote the program, but if anyone else wants to understand it, you will have to explain it to them. It's your house rule. If you don't explain it, others will get frustrated trying to figure it out.
And no one will want to read your code. No one will want to play your weird game with all of the house rules. We don't care if you think your game is good, it takes too long to learn to play, and no one else understands it.
Use the right tool for the job. Do whatever it takes to make your code as lucid and elegant as possible, then hold your head high. Husband, computer programmer, yoga teacher, cat servant.
Coding tutorials and news. The developer homepage gitconnected. Sign in. Situations where labeled branching makes your C code better.
Don Cross Follow. Level Up Coding Coding tutorials and news. Level Up Coding Follow. Written by Don Cross Follow. More From Medium. Nishaanth K. GIT Mono-branch workflow: Pre-tested commits. Introduction to Project Lombok. Spice up your Rest Assured project.
Mitul Vaghela. How to roll out Scrum where it has never been used before. Daria K. Piumal Rathnayake in Level Up Coding. Naina Chaturvedi in DataDrivenInvestor. Your first software engineering job: an ultimate guide.
0コメント