Shop OBEX P1 Docs P2 Docs Learn Events
using Goto — Parallax Forums

using Goto

BaggersBaggers Posts: 3,019
edited 2010-05-31 17:01 in Propeller 1
Why are people so against goto?

Try to write a piece of PASM without a JMP, you just couldn't do it, so why is goto classed as a bad practice?

EG, why would you slow the program down having to traverse past more "if blah then blah" when you've already executed the code you wanted to do, when a goto would take it past them [noparse]:)[/noparse]

Forgive my "new basic code of practice" noobness, I'm just wondering why it's become such a big no-no.


Oh another question, since we have people on here who have written C compilers, why has nobody ever put in an incbin "blah.bin" ( like FILE "blah.bin" in spin )

Again sorry for my questions, please don't confuse them with rants, as you can't pick up conversation tone with text, so... they aren't rants, just curiosity. [noparse];)[/noparse]

Baggers.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

·

Comments

  • heaterheater Posts: 3,370
    edited 2010-05-31 11:00
    A good question Baggers and one that has puzzled me for thirty years.

    There is nothing wrong with GOTO. Lateral and free thinkers everywhere have always thought so but the "structure police" keep them shut up in the closet. Until one day one of them dared to brake free...
    Linus Torvalds said...

    No, you've been brainwashed by CS people who thought that Niklaus Wirth
    actually knew what he was talking about. He didn't. He doesn't have a
    frigging clue.

    More on the use of GOTO in the Linux kernel here: kerneltrap.org/node/553/2131

    It's well used in kernel device drivers. Say your driver needs to claim resources A, B, C, D... before starting it's work. Then when it is done it must release the resources ..D, C, B, A. Well what happens when a resource claim fails, C say? Then you want to abort the start up immediately, free resource B and A only and exit. This is best sorted out with GOTOs. There are many examples in the kernel.

    I'm a sad example. My whole professional career I only managed to get one GOTO past a quality control group. Justified on the grounds of performance in a time critical interrupt handler.

    As for including binary blobs in C. Well I guess it might add some convenience to the normally cumbersome, contorted and hairy way C programs are normally built with Make files, assemblers, linker scripts, object dumpers etc. But when you really want to do it you can just write a quick program or script that converts your binary blob file into a C declaration of an array full of HEX and then compile that into the code with all the other stuff. Add that to you Makefile and you will never miss #includebin "binary.blob".

    I guess the idea is that C is supposed to be a language not a means of gluing binary blobs together. That's what linkers do.

    Of course if your C code is running on a normal OS there is no point. Just add 10 lines of code to your program to open the binary file and read it in at run time.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • BaggersBaggers Posts: 3,019
    edited 2010-05-31 11:08
    Thanks for your insight on this [noparse]:)[/noparse]

    And nice one getting a GOTO past QC lol [noparse]:)[/noparse]

    The incbin was more for none OS systems, yes it's easy to just open and read at runtime when running on an OS machine [noparse];)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • heaterheater Posts: 3,370
    edited 2010-05-31 11:25
    Yep. QC guys on military and avionics projects can be very fussy.

    Now the thing is, all algorithms can be expressed with the normal if,else,while,for,repeat kind of constructs. So it could be said that GOTO is not required.

    There have always been attempts to do automated analysis of code to help in looking for bugs or do timing analysis. Or simply to try and prove that one way function is written is actually equivalent to some other way the function can be written. Formal proofs and verification as it were. I suspect that the use of GOTO makes those automated systems impossible.

    So I support the idea of GOTOless programming as an ideal for expressing your problem. Where it all gets unstuck is when your beautifully structured algorithm has a dozen places at which it could fail which are not really anything to do with the algorithm. A memory allocation fails, or a file open or access or whatever.

    In the face of these messy practical things one can start loading up the code with millions of "if (result != OK) bla bla" to protect yourself against the whole thing collapsing or just producing wrong results. This quickly becomes a mess of nested ifs and now all that error handling code obscures the beautiful structure of the algorithm you started with.

    The "structure police" grappled with this issue for a long time and eventually came up with a brilliant "structured" solution in "exceptions". Great now we have GOTO dressed up as "throw exception" or whatever in C++, Java etc. Yuk.

    Torvalds saw it for what it was. You want to get out of your nice structured code as fast as possible without screwing up the structure of the code. Without making it unreadable. GOTO is your friend here.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.

    Post Edited (heater) : 5/31/2010 11:30:27 AM GMT
  • BaggersBaggers Posts: 3,019
    edited 2010-05-31 11:36
    indeed, goto is a friend, I just find it funny how they think lots of unnecessary error handling if(result != OK)'s make things look messy and slow, I'd rather have a goto.
    like for getting outside of nested for loops etc

    also, now I think of it, why hasn't C adopted a jump table yet? instead of using switch for some jump table that has an offset that is anded off to a nice number.

    yes I know you can do one, but people seem to prefer switch and case statements, I guess it looks tidier than the mess that you have to do to fudge a jump table.

    I guess I'm an old coder who likes speed over how nice/readable the code looks, maybe cos that's what my job has required of me for the last 25 years [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • BradCBradC Posts: 2,601
    edited 2010-05-31 11:39
    Goto has its uses.

    brad@bklaptop2:/tracks/devel/Projects/Pascal/bst/compiler$ grep Goto * | wc -l
    30

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Are you suggesting coconuts migrate?"
  • BaggersBaggers Posts: 3,019
    edited 2010-05-31 11:40
    [noparse]:D[/noparse]

    Edit: sorry that wasn't much of a reply, but you're post put a smile on my face and I had to share it [noparse]:)[/noparse] good to see even C Compilers use GOTO.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • heaterheater Posts: 3,370
    edited 2010-05-31 11:51
    I think you will find that most C compilers will generate a jump table for a switch statement. That is if your cases are consecutive 0, 1, 2, 3... not necessarily starting at zero. So speed wise a hand made jump table may not save you much.

    An array of functions makes a good jump table in some cases. But then you have the overhead of a call and return and the limitation of not being able to do a GOTO some common exit point after each case.

    Then there is the nightmare of setjmp, longjmp en.wikipedia.org/wiki/Setjmp.h

    Still, if you feel that C is like programming in a straight jacket you should try ADA[noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • heaterheater Posts: 3,370
    edited 2010-05-31 11:54
    I think BradC is referring to his BSTC comiler which compiles Spin and is written in Pascal.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • BaggersBaggers Posts: 3,019
    edited 2010-05-31 11:56
    rofl, yeah I guess you're right, and I should put some more faith into C optimisers also, they used to be a nightmare removing many parts of VERY IMPORTANT code.

    like for example, on the saturn, ( yes a few years back ) I tried the C compiler optimisations, and the small part of code that set the palette, RGB bytes, were read using a *ptr++ yet, when it optimised it, it decided to remove the ++ on the first two, ( R & G ) so it ended up greyscale, so from that day on, I tried to keep away from C optimisers, as much as I could, not trusting the code they output. especially when writing games, you need to know what your code is doing. ( maybe not all game coders think my way lol ) but I like to know what I write, is what code is being generated [noparse]:D[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • kwinnkwinn Posts: 8,697
    edited 2010-05-31 12:09
    I agree there are times when having a GOTO available would make things a lot simpler and easier.

    OTOH if you have ever had to update/modify a large spaghetti code program that uses a lot of GOTO's you might have a different opinion. The CASE, IF, IF THEN ELSE, REPEAT, etc. work well in most cases and you rarely need GOTO. Even on those occasions where a GOTO would be nice, there is usually a simple workaround.

    A jump table instruction for C is a great idea. I wrote some software for a company that had a very simple macro compiler/translator for a C like language that translated a few of the structured instructions into assembly. One of those was a table jump (TJMP) instruction. Amazing how much something so simple could speed up assembly programming and make for faster run times.
  • heaterheater Posts: 3,370
    edited 2010-05-31 12:25
    If your C code writes some data to some variables or writes to memory via a pointer but it never reads the same back again then as far as the optimizer is concerned this is redundant code. After all what is the point of having A=2; if A is never used again? This is all quite reasonable I think you'll agree.

    The problem comes when you want that variable or memory to be written to because you know that something outside of your C code is going to be reading it. Could be a memory mapped peripheral register or an area of video RAM or whatever.

    In that case you should tell the compiler, by using the "volatile" keyword on the variable declarations, that there is more going on with that data than it knows about. This will force the optimizer to write out otherwise unused data.

    Not so sure about your example though. Could be that reading from some register that is not written to by some previous code is considered by the optimizer to be the same as reading an uninitialized variable. In which case returning some random junk in an optimized way is correct[noparse]:)[/noparse]

    Again I bet "volatile" would have put it right.

    That's not to say that optimizers don't have bugs, I suspect most compiler bugs have been found in the optimisers.

    In fact back in those military/avionics projects sometimes switching on optimizations was not allowed. Reson being that they would like to take the assembler output of the compiler and have some someone review it, what a job[noparse]:)[/noparse]. Especially the crypto handling parts. Also it is much easier to debug non-optimized code. But having debugged and tested it you cannot then turn on optimizations as it would no longer be the same binary code you are shipping.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • BaggersBaggers Posts: 3,019
    edited 2010-05-31 12:32
    kwinn, yeah it is amazing how dated C is in terms of advancing with code [noparse]:)[/noparse] although don't get me started on C++ and classes, I like access any way I want and I don't like private funcs, but that's my personal feeling, as I like to know what my code is doing, and how it's doing things.

    heater, yeah I know about volatile [noparse]:)[/noparse] great little annoyance if you don't lol especially with interrupts lol don't get me wrong it might have made a great optimisation, but not when it was needed. lol.

    yes that's the trouble with optimisations, if you change a setting, you've changed the WHOLE application.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • Mike GreenMike Green Posts: 23,101
    edited 2010-05-31 13:42
    If you go back and read the original papers on GOTOless programming, you'll find that the main complaint was that GOTOs were used too often where other constructs could just as easily be used with greater clarity. There was a great flurry of demonstrations of compilers and operating systems written without a single GOTO as demonstrations of very complex programs that didn't need them. Pascal was designed as an example of a general purpose programming language that had only a limited local GOTO.

    Wirth made the point repeatedly that beginners should be taught how to program without GOTOs and, only when they have become experienced, should they begin to use GOTOs, presumably in situations where their use clarifies the intent of a program.

    Post Edited (Mike Green) : 5/31/2010 2:38:33 PM GMT
  • BradCBradC Posts: 2,601
    edited 2010-05-31 13:49
    Mike Green said...
    Wirth made the point repeated that beginners should be taught how to program without GOTOs and, only when they have become experienced, should they begin to use GOTOs, presumably in situations where their use clarifies the intent of a program.

    Interestingly this has made me go back and have a look at the 30 goto's in bstc.

    In every case they are used in larger case structures, or nested blocks where a common error recovery path is required. Rather than duplicate the same code for the abort, they use the goto to jump to the same point in the routine for the abort. So it's a cheat used purely to assist in code consolidation.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Are you suggesting coconuts migrate?"
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2010-05-31 13:57
    I use GOTO all the time. Ever since I learned how to do it in SPIN I have used it multiple times. Sure, I can code without it, but GOTO let's me split up the commands into nice, neat chunks that are easier to use.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Check out my new website!!

    Use the Propeller icon!! Propeller.gif

    Follow me on Twitter! Search "Microcontrolled"
  • BradCBradC Posts: 2,601
    edited 2010-05-31 13:59
    Microcontrolled said...
    I use GOTO all the time. Ever since I learned how to do it in SPIN I have used it multiple times.

    ?!?
    Goto?? SPIN ??

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Are you suggesting coconuts migrate?"
  • heaterheater Posts: 3,370
    edited 2010-05-31 14:51
    BradC: "In every case they are used in larger case structures, or nested blocks where a common error recovery path is required. Rather than duplicate the same code for the abort, they use the goto to jump to the same point in the routine for the abort. So it's a cheat used purely to assist in code consolidation."

    I don't think it's a cheat at all. In order to aid in maintenance code should not be repeated needlessly. One day you might fix a bug in one copy and not notice it exists in the other. Besides it helps future generations not having to read so much code to understand what goes on.

    If structured, GOTOless programming means needles repetition of code then Torvalds was right to reject it.

    Mike Green: "Wirth made the point repeated that beginners should be taught how to program without GOTOs and, only when they have become experienced, should they begin to use GOTOs, presumably in situations where their use clarifies the intent of a program"

    Now I'm convinced Torvalds was right. "He [noparse][[/noparse]Wirth] doesn't have a frigging clue."

    When I first learned programming we were taught assembler almost from the beginning. The idea was to teach us about what a computer is what it does at heart and how it does it. We started pretty much at the bottom and worked up.

    There are many here I think who have grown up to be excellent programmers having started out serious programming on 8 bit machines in assembler.

    GOTOs were everywhere for us.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-05-31 14:53
    GOTOs don't kill programs, programmers do. tongue.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • StefanL38StefanL38 Posts: 2,292
    edited 2010-05-31 15:03
    @computerguy can you post a code-example how you use goto in SPIN?

    about goto I totally agree with Mike Green. Learn to porgram WITHOUT ANY goto.

    Then if you are an experienced programmer use it sometimes if it really makes sense.

    If you use goto as a beginner this leads straight forward to spaghetti-code.

    best regards

    Stefan
  • John AbshierJohn Abshier Posts: 1,116
    edited 2010-05-31 15:06
    I once was reading a paper listing of a FORTRAN program. I came upon the line GOTO 1330. I spent an hour looking for the label 1330. It was the line after the GOTO.

    John Abshier

    P.S. For much of my early programming we did not have editors so a search was out of the question.
  • BeanBean Posts: 8,129
    edited 2010-05-31 15:15
    This whole notion of "spaghetti-code" is way over blown. And is just an excuse of the "anti-GOTO" crowd.

    Sure I've seen some code that over-used GOTO, but whoever wrote that code is NOT going to write beautiful structured code just because you take away GOTO.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Use BASIC on the Propeller with the speed of assembly language.
    PropBASIC thread http://forums.parallax.com/showthread.php?p=867134

    March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There are two rules in life:
    · 1) Never divulge all information
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    If you choose not to decide, you still have made a choice. [noparse][[/noparse]RUSH - Freewill]
  • bill190bill190 Posts: 769
    edited 2010-05-31 15:58
    With C programming, you can create a "function" (module) and stick that function/module down below the main program. Or even stick it in another file altogether. Keeps the clutter down.

    These functions can be your own little "commands" which do something.

    For example·today I am working on a parity checking function for serial communications (As in setting the serial port parity to None, Even, Odd, etc.)

    In·the data sending part of my program, I can just list each thing I am doing by using functions. So something along the lines of...

    GetNextByte(Data);
    ParityCheck(Data, Parity);
    SetNinthBit(Parity);
    putcUSART(Data);

    You can give each function a name which clearly describes what it does. Then when you look at the main program, it is much easier to see what is going on. Especially for someone else looking at the program or myself 2 years from now when I have forgotten what I did.

    Then so far as sending this serial data, there is all sorts of stuff to do and check with the USART. I can concentrate in the main program on the USART·aspect and clearly see that each step is as it should be. Or easily add an additional check/step.

    So in the above I'm not checking to see if the send register is clear. I can easily add another function/command and give it any name I want. So maybe call it WaitTxRegOkToWrite(); Then stick that in before I write anything to the USART. And the code for that would be down below or could be in another file.

    Basically each of those *is* a fancy "goto". But it also can send information (parameters/arguments) along with it and send information back.

    AND... I can also then stick all the above in its own function in a different file and call it SendData().

    So when I can do all that, I never see any reason to use a goto...
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-05-31 16:39
    I absolutely do not miss having GOTOs in Spin. If you think you need a GOTO, you almost certainly need to reorganize your thoughts and refactor your algorithm. For me, any latent desire for a GOTO comes when modifying a program -- not in the initial write. Without them, I'm sometimes forced to restructure the code entirely, rather than simply to patch it. And thats a GOOD THING. Even when programming in assembly, I'm thinking in terms of IF/THEN, SELECT/CASE, and REPEAT. To do otherwise is a slippery slope to spaghetti code purgatory, even for an experienced programmer. When I think of the GOTO-infested stuff I wrote as a beginning Fortran programmer, I cringe, recognizing the GOTO now as a crutch for the lazy, disorganized, or inexperienced.

    Niklaus Wirth was spot on. It's Linus Torvalds (an otherwise fine programmer) who's lacking in the clue department regarding GOTOs.

    -Phil
  • jazzedjazzed Posts: 11,803
    edited 2010-05-31 17:01
    Do whatever makes sense for whomever has to maintain/enhance your code.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    May the road rise to meet you; may the sun shine on your back.
    May you create something useful, even if it's just a hack.
Sign In or Register to comment.