Monday, May 24, 2010

Why is goto in programming frowned upon?

Being a hobby programmer (c++ and any other language I feel like learning) I have read from numerous source that using the


goto keyword is a bad thing and should be avoided.





From what I've read it is avoided because it basically makes your code hard to read/understand, but I think that if used properly it can be usefull.





I also dabble in assembly, and was getting confused with the jmp until I learned about the goto and then it hit me....





jmp is almost exact to goto.





So wouldn't goto help people who want to learn oh say assembly??





I have also found goto helpfull in rare cases.





what about you??

Why is goto in programming frowned upon?
Goto's can make programs hard to read and understand. That's a good enough reason not to use them. Would Stephen King reference page 217 in his novel if you are at page 41? Exactly.
Reply:Even as a hobby programmer - do you expect to ever have to work on your code after you finish it? Unless you leave yourself extensive comments,





if (x == y) goto z;


a few pages of code


z:





is a lot more confusing than





if (x %26lt;%26gt; y) bypass = true;


if (!bypass) {


    a few pages of code


}
Reply:If you or your team are the only ones who will see the code, then I do not see a problem. If you plan on releasing the code to other people, then it would be best to use other methods.


No comments:

Post a Comment