Click here to Skip to main content
15,900,325 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: goto statement Pin
altomaltes10-Nov-13 20:17
professionalaltomaltes10-Nov-13 20:17 
GeneralRe: goto statement Pin
PIEBALDconsult10-Nov-13 4:47
mvePIEBALDconsult10-Nov-13 4:47 
GeneralRe: goto statement Pin
altomaltes10-Nov-13 20:23
professionalaltomaltes10-Nov-13 20:23 
GeneralRe: goto statement Pin
PIEBALDconsult10-Nov-13 5:04
mvePIEBALDconsult10-Nov-13 5:04 
GeneralRe: goto statement Pin
CPallini10-Nov-13 6:13
mveCPallini10-Nov-13 6:13 
GeneralRe: goto statement Pin
  Forogar  10-Nov-13 6:23
professional  Forogar  10-Nov-13 6:23 
GeneralRe: goto statement Pin
CPallini10-Nov-13 6:30
mveCPallini10-Nov-13 6:30 
GeneralRe: goto statement Pin
Bill_Hallahan10-Nov-13 6:31
Bill_Hallahan10-Nov-13 6:31 
The book, "Classics In Software Engineering" has Dijkstra's excellent paper, "The Case Against The Goto". There was also another paper by another author in that book stating situations where the goto is useful. A goto is worthwhile in some very limited contexts.

I found a related very short paper by Dijkstra online that is titled, "A Card Against The Goto" at http://www.cs.utexas.edu/users/EWD/transcriptions/EWD02xx/EWD215.html[^]. It is more of a philosophical outlook about the issue and doesn't go into the depth of the article in the book.

It's also worthwhile reading the paragraphs under "Considered Harmful" at http://blogs.perl.org/users/erez_schatz/2011/07/rewriting-the-language.html[^]. That section tells about how when Dijkstra's article, "The Case Against The Goto" was submitted to the ACM magazine, the editor changed the title [to "Gotos Considered Harmful"] and created a furor! Two key sentences at the link above are:

"It should cease to exist. Nothing in programming is definite. There is no single element that is either a silver bullet or the Antichrist."

However, it is true that, in the vast majority of cases, using a goto can and should be avoided. But it's also a mistake to revile any code that contains a goto merely because the code contains one.

By the way, I also agree with another poster that multiple returns in a function are undesirable, although I can see exceptions for this too. A single return in a function makes debugging so much easier. I make a serious effort to have only a single return, however, I have broken this guideline at times, particularly when working on critical legacy code where I wanted to minimize changes to the code.

Here's a construct in pseudo-code that I've used in both C and C++ programs to avoid the need for gotos for multiple error cases. (By the way, I also always put the parenthesis in a statement, even for only one-line statements, because it makes the code easier to maintain. Typically, 85% of the cost, or time, spent on code is maintenance, so typically, code should be written to make it easy to maintain, as opposed to making it easy for the person writing the code).

I did not invent this technique and I don't know who to credit for this idea.
C++
// Some code here.  Entering a section with lots of error checking.

do
{
    // Some code goes here that sets an error condition.

    if (error)
    {
        break;
    }

    // Some more code that sets an error condition.

    if (error)
    {
        break;
    }

    // Even more code that sets an error condition.

    if (error)
    {
        break;
    }

    // etc.
}
while (false);

// The code continues here.

That construct avoids the extreme indenting that can occur with a lot of nested error checks. Each 'break' statement acts like a goto that sends control to the first statement following the end of the do loop. And, of course, it's not really a loop at all because of the "while (false)" at the end. I believe that most compilers will optimize the loop away.

modified 10-Nov-13 12:51pm.

GeneralRe: goto statement Pin
  Forogar  10-Nov-13 6:51
professional  Forogar  10-Nov-13 6:51 
GeneralRe: goto statement Pin
Bill_Hallahan10-Nov-13 7:05
Bill_Hallahan10-Nov-13 7:05 
GeneralRe: goto statement Pin
Rob Grainger10-Nov-13 22:50
Rob Grainger10-Nov-13 22:50 
GeneralRe: goto statement Pin
Bill_Hallahan11-Nov-13 5:05
Bill_Hallahan11-Nov-13 5:05 
GeneralRe: goto statement Pin
Rob Grainger11-Nov-13 8:28
Rob Grainger11-Nov-13 8:28 
GeneralRe: goto statement Pin
Bill_Hallahan11-Nov-13 13:51
Bill_Hallahan11-Nov-13 13:51 
GeneralRe: goto statement Pin
Rob Grainger12-Nov-13 2:00
Rob Grainger12-Nov-13 2:00 
GeneralRe: goto statement Pin
Tarek Elqusi10-Nov-13 6:53
professionalTarek Elqusi10-Nov-13 6:53 
GeneralRe: goto statement Pin
Marc Clifton10-Nov-13 9:58
mvaMarc Clifton10-Nov-13 9:58 
GeneralRe: goto statement Pin
Joe Woodbury10-Nov-13 10:14
professionalJoe Woodbury10-Nov-13 10:14 
GeneralRe: goto statement Pin
Christian Graus10-Nov-13 12:01
protectorChristian Graus10-Nov-13 12:01 
GeneralRe: goto statement Pin
Member 1008817110-Nov-13 12:55
Member 1008817110-Nov-13 12:55 
GeneralRe: goto statement Pin
Stefan_Lang11-Nov-13 1:42
Stefan_Lang11-Nov-13 1:42 
GeneralRe: goto statement Pin
vl211-Nov-13 8:01
vl211-Nov-13 8:01 
GeneralRe: goto statement Pin
Stefan_Lang12-Nov-13 1:15
Stefan_Lang12-Nov-13 1:15 
GeneralRe: goto statement Pin
vl212-Nov-13 1:30
vl212-Nov-13 1:30 
GeneralRe: goto statement Pin
werinus12-Nov-13 1:34
werinus12-Nov-13 1:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Straw Poll

Were you affected by the geomagnetic storms this past weekend?
Communication disruptions, electrified pipes, random unexplained blue-screens in Windows - the list of effects is terrifying.
  Results   483 votes