Solved

A query pins me some time

Posted on 2004-11-01
207 Views
Last Modified: 2010-04-17
Hello experts,

I have a question (may be confusion)

Text books says "Using GOTO statement is a bad programming Practice"
they added "U lost control over ur program by using it."

But practically we found that there is time when one has to use it.
On the top i am confused why its is bad to use ? how we lost control over code by using it ? and ok. if it is , then why
it is provided from QBasic to VB 6 .....may be in .Net.

(By the way I am a VB programmer)

Thanx
0
Question by:sandygad
    6 Comments
     
    LVL 23

    Expert Comment

    by:brettmjohnson
    The lack of exception handling in ANSI C forced us to use goto in error situations
    where we had to clean up resources (allocated memory, open files etc).  The
    typical code looked like this:

    aStruct * aFunc( ... )
    {
          aStruct rtn = NULL;
          char * mem = NULL;
          FILE * fd = NULL;
          int err = 0;
    ...
          if (!(fd = fopen( ...))) goto abort;
    ...
          if (!(rtn = (struct aStruct *))) goto abort;
    ...
          if (!(mem = (char *)malloc(..))) goto abort;
    ...
          /* success path */
          return rtn;

    abort:
          if (fd) close(fd);
          if (mem) free(mem);
          if (rtn) free(rtn);
          return NULL;
    }

    Of course the gotos could be avoided using deeply nested if-then-else constructs,
    but the code became unmanagable as well as unreadable.   The in-line alternative
    cleanup:
          if ((mem = (char *)malloc(...)) == NULL) {
                fclose(fd);      /* close the open file */
                free(rtn);      /* free our result obj */
                return NULL;
          }
    is highly error-prone and clutters the flow with lots of redundant cleanup code -
    obfuscating the true logic path with chunks of code that will hardly ever get
    executed.



    0
     
    LVL 7

    Accepted Solution

    by:
    http://www.acm.org/classics/oct95/


    Have a review on this article...
    0
     
    LVL 48

    Expert Comment

    by:Julian Hansen
    From what I understand the goto statement allowed (knowingly or otherwise) programmers to bypass code that could result in the application becomming unstable. For instance

      Open a file
        Read a value from the file
        Is the file structure correct No then Goto Lable1
      Close file
      Exit
    Lable1
       Dispaly error
       Exit

    In this simple example the goto jumps over the close file leaving an orphaned resource.

    This type of error can be easily avoided but in complex code using goto too many times could result in totally unmanageable code.

    In our university courses we were taught "Use goto ONLY when you have to and then ONLY in a forward direction" in other words never use goto to jump to a previous point in the code.

    That's my 2c worth - you will probably find a lot more info in the article listed by murugesandins above.
    0
     
    LVL 16

    Expert Comment

    by:PaulCaswell
    The essential problem of 'goto' (and sometimes 'break', 'continue' etc) is for the future programmer.

    Take the sample code above:

    aStruct * aFunc( ... )
    {
         aStruct rtn = NULL;
         char * mem = NULL;
         FILE * fd = NULL;
         int err = 0;
    ...
         if (!(fd = fopen( ...))) goto abort;
    ...
         if (!(rtn = (struct aStruct *))) goto abort;
    ...
         if (!(mem = (char *)malloc(..))) goto abort;
    ...
         /* success path */
         return rtn;

    abort:
         if (fd) close(fd);
         if (mem) free(mem);
         if (rtn) free(rtn);
         return NULL;
    }

    Imagine it is written and embedded in a program and stays there, stable and acceptable.

    ...

    Now, a few years later, a novice programmer wishes to allocate another block of memory or open another file. Remember that the '...' can be many lines of code. It is not obvious, not even clear, what should be done if the allocation failed.

    If, however, the code had been written:

    aStruct * aFunc( ... )
    {
         aStruct rtn = NULL;
         char * mem = NULL;
         FILE * fd = NULL;
         int err = 0;
    ...
         if (fd = fopen( ...))
         {
    ...
         if (rtn = (struct aStruct *))
         {
    ...
         if (mem = (char *)malloc(..))
         {
         free(mem);
         }

         free(rtn);
         }

         close(fd);
         }
    ...
         /* success path */
         return rtn;
    }

    then it is obvious what must be done!! In fact, almost any modification of the original code if fraught with danger.


    Paul
    0
     
    LVL 1

    Expert Comment

    by:marban
    In visual basic 6 you only should use goto with an on error clause. If goto is used in other context, the code it's dificultly to read and understand.

    I hope i help you.
    Bye, and sorry for my spell, i don't konw so much english.

    Marcelo
    Montevideo - Uruguay

     
    0
     
    LVL 1

    Author Comment

    by:sandygad
    Hi All

    Thanks for your valuable comments.
    I read them all & found more or less from each one.

    Regards
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    Title # Comments Views Activity
    How to inactivate a tab on a form based on permissions? 11 50
    countClumps  challenge 10 52
    Visual xHarbour 1 30
    find a node in VST 2 24
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    A short article about problems I had with the new location API and permissions in Marshmallow
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now