The Lounge is rated PG. If you're about to post something you wouldn't want your
kid sister to read then don't post it. No flame wars, no abusive conduct, no programming
questions and please don't post ads.
The important thing with goto is knowing when not to use it - which is most of the time.
Goto has its place, but if you can use a structured loop or branch instead then your code is generally cleaner and more maintainable. But very occasionally, using a goto makes your code cleaner or much more efficient and (provided it's well documented) isn't a problem.
It's a problem when it's used because they can: and teachers who introduce it early should be hung, drawn, and quartered (and I don't mean "well hung", "sketched", and "given an apartment").
Sent from my Amstrad PC 1640 Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
The only time "goto" is warranted is if in a nested structure where to accomplish the code control, a flag would need to be set and then checked more than once by the control statements within which this decision to break out is needed.
I think I have used it 3 times, and all times, I gave a very good comment as to why it was the proper choice.
goto is NOT the enemy, it's only when it's used wrongly.
... contrary to popular belief disguising it fixes nothing
... if you really need to GOTO, just do it - hiding it only increases the bullshit level.
unfortunatelly, there are teachers that tell the kids "GOTO is bad," but then "to do the same thing..." they then go on and teach the kids how to disguise them, i.e. instead use THROW, or stick the [inner] code in a method and return early (i.e. still in an indefinite state). ffs, really!
I've got no problems with goto,
but I do have issues with (1) sh*t code, and even more (2) attempts to hide sh*t code
Well said! Not using GOTO has become a matter of faith rather than good programming practice. I've seen some totally impenetrable code that has a indeterminate state, all because of the convoluted efforts the devs went through to avoid a GOTO. (Including rafts of local variables called things like a, aa, aaa, aaaa, x, xx, xxx, xxxx etc!)
GOTO is just another tool - it can be used appropriately or badly, just like any other...
teachers who introduce it early should be hung, drawn, and quartered (and I don't mean "well hung", "sketched", and "given an apartment").
My CS teacher introduced them early. However it was more of a "Here is the goto statement, here is how it can go horribly wrong, and that is why you should never use it again... ever. If you do, you will fail whatever assignment it was in." sort of way.
While it was a bit over dramatic, she did follow up with how to better handle branching/looping logic without them.
A lot of these prohibitions are in reality "almost never do such and such." But at the end of the day, other things being equal, it's all about clarity and readability and there will be cases where it's fine. The language directive does exist after all (when it does).
Years ago I recall reading a section in the book Code Complete by Steve McConnell where he presented some code using goto and stats showed that almost no developers were able to rewrite it correctly without the goto.