LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-20-2013, 10:06 PM   #31
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,397

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814

Quote:
Originally Posted by miriam-e View Post
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).
 
Old 03-20-2013, 10:38 PM   #32
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 510
Blog Entries: 2

Rep: Reputation: 68
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.
 
Old 03-21-2013, 08:23 AM   #33
miriam-e
LQ Newbie
 
Registered: Nov 2011
Location: QLD, Australia
Distribution: Puppy Linux
Posts: 12

Rep: Reputation: Disabled
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.
 
Old 03-21-2013, 08:32 AM   #34
miriam-e
LQ Newbie
 
Registered: Nov 2011
Location: QLD, Australia
Distribution: Puppy Linux
Posts: 12

Rep: Reputation: Disabled
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.
 
Old 03-21-2013, 09:41 AM   #35
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,053

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
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]

jlinkels

Last edited by jlinkels; 03-21-2013 at 09:42 AM.
 
Old 03-21-2013, 10:22 AM   #36
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,397

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814
Quote:
Originally Posted by miriam-e View Post
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.
 
  


Reply

Tags
bash, mac


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
goto/label command for scripting in bash shell terry.trent Linux - Software 3 07-09-2010 10:15 AM
can you use goto in bash script sabliny Programming 3 10-07-2005 05:54 PM
goto usage bru Programming 5 03-09-2004 03:44 AM
Goto command? batfoot Linux - General 4 08-26-2003 07:17 PM
GOTO function in bash? sobchak Programming 1 07-22-2002 05:10 AM


All times are GMT -5. The time now is 09:21 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration