|
Nah, not at all. I have no hate against goto.
I think they are noble and worthy operator.
And really, all those fancy while, for, do, try/finally loops are just fancy goto in disguise!
I just, for some reason, maybe traumatising past experience? Feels the anticipation of painful critics.
This is rather odd since it's my own private code base though...
In fact I did suffer at the hand of anti goto fanatics in the past!
modified 20-Jun-19 8:22am.
|
|
|
|
|
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.
Kevin
|
|
|
|
|
To avoid this you can put the entire code block in a try. Put the catch at the go to label and throw an exception where you call the goto.
Because that’s different somehow!
Socialism is the Axe Body Spray of political ideologies: It never does what it claims to do, but people too young to know better keep buying it anyway. (Glenn Reynolds)
|
|
|
|
|
avoiding goto by using try/catch/exception for expected behavior! genius!
|
|
|
|
|
I was going to chime in that all Exception handling is really goto programming and then I saw DRHuff's excellent reply.
People talk about never using goto and then you look at their code and see exception handling and think, "what's this here?"
This is also why exception handling can be a pain in the arse. All that jumping around.
Consider what happens if you throw an exception in your catch block -- and it is possible. Things can get really ugly jumping here, bouncing there.
|
|
|
|
|
I think the only time that exception handling should be used is when the current code section does not have any control over the code that is throwing the exception - i.e., it is preferable to check and control something than to rely on an exception that simply does a goto the catch branch.
|
|
|
|
|
I can agree with that.
I had the misfortune of having to deal with an automation systems framework that was essentially a state machine library and it changed states by throwing an exception. I thought it was incredibly stupid. This framework was spreading throughout one division of a customer until someone finally pulled their head out and asked what is this pile you have subjected us to?
This particular customer joined my very short list of the worst ones I have ever dealt with and I refuse to buy any of their products to this day.
"They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"
|
|
|
|
|
agreed. I'm thinking of calls to another library where it throws a specific exception after being configured properly. In that case all inputs are set to valid values but then something happens which causes the exception - network not available or something.
|
|
|
|
|
And what happens when the language you are programing in offers no try-catch-throw?
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
|
I cannot tell a lie, I used one once. many years ago.
I felt awful. I added a twenty line comment apologising for it.
I went to bed in trepidation but awoke the next morning to find that I hadn't been struck down dead by the code gods,the fix had worked, the customers were happy and nobody had died.
Not sure I'd risk it again, though
Whenever you find yourself on the side of the majority, it is time to pause and reflect. - Mark Twain
|
|
|
|
|
PeejayAdams wrote: many years ago
PeejayAdams wrote: and nobody had died That's the exact same time my cat died!
[Dramatic pause]
I don't know who you are. I don't know what you want, but I have a very particular lack of skills. I will never be able to find you. But what I do have is two dollars and a Casio wristwatch. You can have one of them.
|
|
|
|
|
I'm sure it was the week after your cat died but I'll have the Casio just in case!
Whenever you find yourself on the side of the majority, it is time to pause and reflect. - Mark Twain
|
|
|
|
|
|
Bully for you.
I used one yesterday when my doorbell camera showed some people, with little booklets in their hands, at my door.
If you can keep your head while those about you are losing theirs, perhaps you don't understand the situation.
|
|
|
|
|
Aw man... you can't just drop that on us and not show us the code! That's just mean!!!
|
|
|
|
|
Mm.. it took me 2 hours to write the function.. that doesn't have goto in it in the end!
To be fair both the function was complicated and I was distracted!
|
|
|
|
|
it's an old view but as a Hardware/Firmware type I cannot see the hatred for goto, I mean the jmp is a direct map and it produces a smaller binary. Goto is like an axe very useful but very dangerous. An axe wont cut your hand off, misuse it and you could be missing a hand very easily!
|
|
|
|
|
Obligatory XKCD[^]
GOTO is perfectly fine in some scenarios. It's a language feature not a bug. I have used maybe 4 over the past 15 years. So not common, but absolutely has its place.
|
|
|
|
|
Why not use them? Sometimes they make sense.
Spaghetti code is spaghetti code regardless whether it uses classes, is structured, or just plain crap.
|
|
|
|
|
Coming from a embedded back ground, GOTO maps directly to JMP! write assembly without JMP. Like you said I battled with object oriented code that is c*ap but well structured. I like anything don't blame the tools, blame the tool using the tool!
|
|
|
|
|
I have seen some terrible C++ code, the worst was a child of a parent, instantiated as the parent.
SO child * myclass = new parent.
WTF!
Which frigging function is going to get called, the base class or the overridden one!
I wanted to punch the person who wrote that.
|
|
|
|
|
I once used a "computed goto" - it was in FORTRAN where this kind of thing used to be expected!
GOTO is my go-to command I go to when I need a goto.
- I would love to change the world, but they won’t give me the source code.
|
|
|
|
|
Find In Files / Current Project / "goto"
Matching lines: 1254
it's common in C, as it's the cleanest way to handle error conditions.
|
|
|
|
|
In high level languages... Ok, maybe you have more possibilities and you should avoid using goto's
But try to programm in Assembly, in PLC LAD or other similar "low" level languages without it.
It is like many other things. Use it wisely and there would be no problems. Abuse or misuse it... and you will burn in hell (or you will be damned and sent to hell by the people who inherit your code)
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|