ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash scripts could often be made much simpler, faster, and easier to read by judicious use of GOTO. It is a great pity that it was omitted.
I think GOTO is acceptable in some uses, but if your script is that complicated you should not be writing it in bash (bash is lacking in many more things besides GOTO).
Some programming purists even object to break or early return statements. They believe that each module should have one and only one entry point as well as one and only one exit point no matter how much extra code you need to add to do so.
I agree. The early GOTO linenumber was almost certainly responsible to much of the early dislike, however that objection should never have remained after the addition of GOTO label was introduced. Allowing it to become a blanket ban on a useful tool is pretty-much inexcusable, but there is something in human nature that sometimes lets us shortcut our thinking processes and allow the absurd to somehow become dogma (for example every religion on Earth).
As the rather humorously titled "GOTO Considered Harmful" Considered Harmful by Frank Rubin showed, there are certain situations where GOTOs are not only more efficient than other structures, but also make for much more easily read (and maintained) code.
(See his piece at: http://web.archive.org/web/200903200...ubin87goto.pdf )
For the record, Niklaus Wirth was responsible for the whole problem, as he was the person at ACM who retitled Edgar Dijkstra's paper from the more considered "A Case Against the Goto Statement" to the inflammatory "GOTO Considered Harmful" which stuck in people's minds as a meme. Almost nobody has read the original paper, as it is so dense and abstract; they just remember the title. Niklaus Wirth is immortalised by having created the almost useless language PASCAL as an illustration of his ideas. Modula was a slight improvement, but you'd need your head examined trying to do much with either (in my opinion). It illustrates how creating stuff based upon dogma produces broken results, even when an undeniably brilliant mind like Wirth's is doing it. Programming is still as much an art as it is a science and to ignore that and succumb to dogmatic restrictions is to risk becoming lost in pointless deliberations of how many angels can dance on the head of a pin.
ntubski, I came upon this discussion when I searched for some way to use a simple GOTO type of structure in a small bash script I was writing (only about 60 lines). I had begun to look into ways to rewrite it without GOTO, but it looks like it would double or triple the size of the script and make it absurdly convoluted. All this would have been avoided with just a simple GOTO.
You see, initial complexity has nothing to do with it. There are simply some operations that are very difficult to write other ways.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
I agree with you that the complete absence of a GOTO statement because of dogmatic points of view is undesired.
About a century ago when Dijkstra, Knuth and Wirth and (structured) programming styles was still a hot topic I worked on a project where we had to rewrite programs consisting of 2000-4000 lines of Fortran code each to something readable and maintainable. The original code was saturated with GOTOs jumping to arbitrary lines in the code. At that time I certainly did agree with feelings about banning GOTO statements.
However the code had to be rewritten in the same Fortran version which only control structure was IF/GOTO. Nevertheless we implemented the entire code as if modern IF/THEN/ELSE, WHILE, REPEAT etc control structures existed by using the GOTOs in a highly disciplined and structured way. It was quite possible. So the GOTO statement is not even harmful if used correctly.
More than that (I posted that earlier) in a number of cases, like an emergency break, a GOTO is highly desirable.
It would not be bad had it been included in Bash. Since GOTOs should be used in emergency situations and often leading to program termination you might want to emulate the GOTO with a function call which can do exactly that. Premature exits are not considered neat either, but nor are global variables [in Bash]
ntubski, I came upon this discussion when I searched for some way to use a simple GOTO type of structure in a small bash script I was writing (only about 60 lines).
To me 60 lines is getting too big; the best use of of shell scripts is stringing together a few programs in a pipeline. Anything much beyond that and things get ugly fast.
Quote:
I had begun to look into ways to rewrite it without GOTO, but it looks like it would double or triple the size of the script and make it absurdly convoluted. All this would have been avoided with just a simple GOTO.
Now you've piqued my curiosity, would you mind starting a thread about it?
Quote:
There are simply some operations that are very difficult to write other ways.
Yes, the LR parser's state machine example at the page you linked to is a good one.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.