|
This isn't a programming question.
Anyway... I find it useful in very few situations. It can make for cleaner code if used correctly. Of course, it can also be over used.
|
|
|
|
|
I've not used it in a long time.
There are a few of them in our legacy code, but no new code have them; they are mostly used for quick exit of a function to do cleanup.
Nihil obstat
|
|
|
|
|
Honestly, I have written at least 1 million lines of code since I have used a goto. Although I do remember using a few gotos in the 1990s. Also long gone from my coding is writing x86 assembly code.
John
|
|
|
|
|
In my early days I used them a lot - then I discovered methods and functions and stopped using them.
The only exceptions are certain languages which require goto (e.g. VBScript/VBA error handling - on error goto 0).
In studying computer science I was also told that there was one valid known use of a goto statement, which was in a washing machine with an 8 bit processor where the use of the goto statement allowed for a saving on hardware costs. Sadly I can't remember the detail / can't find more info online.
|
|
|
|
|
In C, or C++, the only situation I can recall using it for was to break out of multiple nested loops.
for (int i=0; i<n; ++i)
for (int j=0; j<n; ++j)
{
...
if (all_hell_breaks_loose())
goto out_of_here;
...
}
out_of_here:
Of course, in Java, Fortran (and presumably C#), one just uses a labelled break statement (exit statement in Fortran) for this purpose - main difference being that the label goes at the beginning of the loop being broken out of, rather than the end.
|
|
|
|
|
DanielSheets wrote: This isn't a programming question.
DanielSheets wrote: t can make for cleaner code if used correctly
How can it not be?
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
I can't ever remember needing it in c#. That said, it's been so long I doubt I'd spot a place where it might make things better.
|
|
|
|
|
This is when you would use a goto in C#. Basically a fall through in a switch statement.
|
|
|
|
|
|
Collin Jasnoch wrote: There is no "correct" usage of it in modern high level languages
I doubt the the absoluteness of that statement.
It is likely there are few cases but even one refutes your statement.
|
|
|
|
|
"correct use" depends on your definition of "correct". However, the main point is that even if at first it makes sense, or even simplifies code, it suffers from the lack of long-tem maintainability:
Unlike other control statements, goto lacks an associated block of control, and thus makes it considerably harder to detect or verify the effect of any change you make to code that may or may not be executed, once or repeatedly, depending on goto statements in potentially several entirely different place(s).
Goto would be much less of a problem if jump labels weren't self-declaring and global: if you could localize the use of a label like variable names, that would greatly restrict the potential (ab)use of goto commands, and thus ease the effort to understand the code and verify changes.
|
|
|
|
|
Stefan_Lang wrote: "correct use" depends on your definition of "correct". However, the main point is that even if at first it makes sense, or even simplifies code, it suffers from the lack of long-tem maintainability:
Just to make it clear I have 40+ years of programming experience and I started before OO existed and used languages where one had to use goto along with using languages where goto didn't exist. And a number in between. I understand not only how goto can be misused but how any number of constructs can be used incorrectly.
I also understand how absolutes are never that. (Where the former is a technical point and the latter part is not.) And that is the context in which I made my response.
|
|
|
|
|
DanielSheets wrote: I find it useful
I am not ashamed to admit that I use it all the time.
I goto the store
I goto the bar
I goto the office.
Many other uses too
|
|
|
|
|
Isyourspacebarfaulty? 'Go to' is two words.
|
|
|
|
|
His second stop was the bar, then other places and to write this post. I am assuming he is still enjoying the effects of the local bar.
Common sense is admitting there is cause and effect and that you can exert some control over what you understand.
|
|
|
|
|
My guess is he goes to those places often and with reason. Thus, other block controls would be more useful. E.g., while he is not at work he is in the bar, etc.
|
|
|
|
|
Common sense is admitting there is cause and effect and that you can exert some control over what you understand.
|
|
|
|
|
In FORTRAN the space is not required. It will work either way. That is odd in itself.
|
|
|
|
|
If we are talking of C# then I cant think of a time I would ever use it , although I am somewhat biased as I have told everyone in my team that I would slaughter a family member if I ever found it in any code .
|
|
|
|
|
Who would put family members in their code?
|
|
|
|
|
In SQL - fairly often to jump to the error handler at the end of our sprocs.
I'll admit there's no good reason we do this, since it's easy enough for us to avoid this with if statements, but it's a pattern used in our original code and so for consistency we stuck with it:
Create Procedure MyProc as
Begin Tran
if @@error <> 0 goto errorHandler
Commit Tran
Return 0
errorHandler:
Rollback Tran
Return 1
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
modified 2-Mar-13 21:29pm.
|
|
|
|
|
burn the heretic
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
That's a pattern I have come across in Oracle PL/SQL and it does the job well.
Of course one can use an if, as you mention, but if it is used judiciously it works well and does not break the logic any more than an else does.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
For this same reason it was used in our C code (kernel, various drivers). Not had to use it yet but I can see some uses if you want the CPU/compiler to execute/optimize properly.
|
|
|
|
|
If that's T-SQL in anything resembling modern SQL Server, there are much, much better constructs for error handling now.
|
|
|
|