Not important, only a curiosity...

This is a discussion on Not important, only a curiosity... within the C Programming forums, part of the General Programming Boards category; Originally Posted by BrownB I think this isn't problematic, and it's clearer and easier to maintain than repeating a "database ...

  1. #16
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by BrownB
    I think this isn't problematic, and it's clearer and easier to maintain than repeating a "database close" and returning on each error catched.
    You could do something as in this piece of pseudo-code:

    Code:
    err= open(database);
    if (!err) {
      err= read(database);
      if (!err) {
        err= dosomething(database);
        if (!err)
          err= write(database);
      } 
      close(database);
    }
    if (err)
      printf("error %i while %s\n", err, getdesc(err));
    It's up to you whether or not you prefer it this way. Other languages like C#, Java or Delphi/Pascal offer exception handling for this situation. I'm not sure about C++, but I think it does, too.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  2. #17
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    How about
    Code:
    if ( open_database() ) {
      if ( read_database() ) {
        // do stuff
      } else {
        // error reading database
      }
      // close database
    } else {
      // error opening database
    }
    More often than not, the sudden urge to use goto is a sign of poor design - for example digging your way out of a large function which should have been split into several smaller functions long ago.

    If your functions are small and have a clearly defined purpose, then there shouldn't be a need for goto.
    A massive rambling function trying to do too much all but makes goto a necessity, but the real fix is to redesign it.

    > it's clearer and easier to maintain than repeating a "database close"
    Well I usually think in terms of matched pairs of functions, something along the lines of
    Code:
    FILE *fp = fopen( "file", "r" );
    if ( fp != NULL ) {
      do_stuff( fp );
      fclose( fp );
    } else {
      // open error
    }
    The success (or otherwise) of do_stuff() doesn't matter to this bit of code, the file is opened in exactly one place, and closed in exactly one place.

  3. #18
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    I try to stay with the principle of one entry, one exit when possible. However there are times when an early out in a function is desirable such as in a recursive function or a function for graphics where the early out means higher framerates.

    I haven't used GOTO since GW BASIC version 1.0.

    The only exception to this is in BASIC's ON ERROR GOTO or ON LOCAL ERROR GOTO. Unfortunately VB 6 didn't find a better way to deal with error handling so they resorted to the goto which is just hideous.

    I think there is another aspect to this debate though. You may actually be working against your compiler when you use a goto here or there. Loops look a lot different in assembler and often times they must short jump to a label which is identical really to a goto. So in theory your goto may actually be ignored by the compiler if it deems that it has already taken care of the goto with its own jump in the code.
    Last edited by VirtualAce; 12-22-2004 at 04:05 AM.

  4. #19
    ---
    Join Date
    May 2004
    Posts
    1,379
    So in theory your goto may actually be ignored by the compiler if it deems that it has already taken care of the goto with its own jump in the code.
    Which brings us back to Salem's point. If you ever find yourself in a situation where you REALLY MUST USE A GOTO, then redesign your code.

  5. #20
    VOX
    VOX is offline
    Deleting... VOX's Avatar
    Join Date
    Oct 2004
    Location
    VA
    Posts
    94
    I know someone whos DIOS language compiler (with over 200,000 lines of code) uses goto as his main loop function. He can update and modify his program with speed and efficiancy without any errors at all. He even made his own keyword that uses a complicated branch-goto thing. I don't know why anyone other than him would want to read his compilers code, so it doesn't matter what he uses.
    Boy you stink, go take a shower before you continue to code. Better do your laundry and spray your chair too.

    The one and only resource for finding information.

    Next version of windows released!!!!

  6. #21
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Which brings us back to Salem's point. If you ever find yourself in a situation where you REALLY MUST USE A GOTO, then redesign your code.
    Yeah which is what I said about 20,000,000,000,000 goto threads ago. Why do we keep responding to these goto threads?

  7. #22
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Bubba
    Yeah which is what I said about 20,000,000,000,000 goto threads ago. Why do we keep responding to these goto threads?
    Anything controversial makes for good reading. Also posters change, so you tend to get a different slant each time.

    I hadn't really ever thought about it, just assumed from previous threads that goto does have the occasional use. But Salem makes a good point, if you structure your code properly, it's necessity may evaporate.

  8. #23
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    Quote Originally Posted by Bubba
    Yeah which is what I said about 20,000,000,000,000 goto threads ago. Why do we keep responding to these goto threads?
    Mmmm... perhaps I'm wrong, but I think that anyone who is bored from a thread, could just let it as it is: I called this "not important", so Bubba, please don't feel bad with me: Googling and seraching is ok, but I love so much speaking!!

    Who doesn't care about this question, well...it's not critical!

    Merry Christmas and goto next year!

    BrownB

  9. #24
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by BrownB
    Merry Christmas and goto next year!
    The goto isn't really neccessary here. Next year will be reached in any case, unless maybe somebody calls die() first.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  10. #25
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    Quote Originally Posted by Nyda
    ...unless maybe somebody calls die() first.
    THIS is the real wish I made...

    Bye!

  11. #26
    ---
    Join Date
    May 2004
    Posts
    1,379
    I think year() is called recursively

  12. #27
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    Yeah, the stack is collapsed when you die, and you end up back at top level !

  13. #28
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Though some in some languages your code is sent to the debugger, and then becomes a tsr (terminate and stay resident) program.

  14. #29
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    However, some times when a process fails to die properly, or when something else goes askew, it ends up a zombie.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #30
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    And then it's child windows have to put it in safe mode...

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Curiosity question...
    By Raigne in forum C++ Programming
    Replies: 2
    Last Post: 04-05-2007, 09:49 PM
  2. For me is very important Please...
    By pitkini in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 02:49 AM
  3. What Is Important In Making Games?
    By Krak in forum C++ Programming
    Replies: 6
    Last Post: 05-07-2004, 08:12 PM
  4. Planned Development Community Idea (important)
    By cozman in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-12-2002, 12:13 PM
  5. Most important element of a game?
    By Eber Kain in forum Game Programming
    Replies: 24
    Last Post: 01-11-2002, 02:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21