using Goto
Baggers
Posts: 3,019
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
·
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
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...
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.
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
·
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
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
·
brad@bklaptop2:/tracks/devel/Projects/Pascal/bst/compiler$ grep Goto * | wc -l
30
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"Are you suggesting coconuts migrate?"
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
·
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For me, the past is not over yet.
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
·
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.
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.
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
·
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
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?"
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Check out my new website!!
Use the Propeller icon!!
Follow me on Twitter! Search "Microcontrolled"
?!?
Goto?? SPIN ??
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"Are you suggesting coconuts migrate?"
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA
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 Abshier
P.S. For much of my early programming we did not have editors so a search was out of the question.
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]
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...
Niklaus Wirth was spot on. It's Linus Torvalds (an otherwise fine programmer) who's lacking in the clue department regarding GOTOs.
-Phil
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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.