Why is goto a bad programming practice?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
RetroTechGuy
Posts: 2947
Joined: Tue 15 Dec 2009, 17:20
Location: USA

#46 Post by RetroTechGuy »

rcrsn51 wrote:Here is the test for a well-written block of code. Start at Line 1 and read sequentially down the page to Line N. At that point, after looking at each line once, do you have a complete understanding of what the code does? If you do, the code is well-written.

This is the point that Dijkstra is making. It is hard enough to make the connection between the static lines of programming code and the real-word dynamic process that they represent. The use of elements like goto's and line labels makes it even more difficult.
So the argument should have actually been against the use of line labels, not the GOTO command specifically (as many other commands also allow, or use line labels)...
The original programmer may be able to follow the logic perfectly well, but he is rarely the only audience.
And, just in writing in any language, the formatting, grammar and correct vocabulary all contribute to "readability" of an article, or a programming structure. If I wrote this text with no formatting, capitalization or punctuation, it would be much more difficult to read.

Line labels, just as GOTOs, should be used sparingly (for one, if you use too many labels, you will forget which ones you have used, as you edit and re-edit a piece of code -- then you have cross-linked branches).

Comments and explanations throughout the code help considerably.

Some years ago, I recall trying to debug a piece of code. About 400 lines into the code, I find a single comment "C We now invert the matrix"... So I go back to the top, and there are no arrays defined at all. I go back down, and discover that the "matrix" is all stored in single variables: w,x,y,z,q,r,s,t,e,... WTF!!!! Yes, it was also extreme "goto" spaghetti code... (I believe that is also where I saw the "If(N) 10, 20, 30..." code).

But for our discussion, if I had a more complex problem than simple text substitution, I would likely use a "10 read() >> goto 10" to load the indeterminate data array (I still must over-specify the array size, under most modern compilers -- at one point, the DEC/VAX compiler permitted the array size be determined inside the program, so the array was expanded or contracted internally, and could be defined through a variable...yes, strange). However, once loaded, I now know exactly how large my array is, so all subsequent calculations can be explicit do loops of "i = 1, size" -- and no further no line labels needed (though I almost always put a line label on the "end" statement, as a termination point).
[url=http://murga-linux.com/puppy/viewtopic.php?t=58615]Add swapfile[/url]
[url=http://wellminded.net63.net/]WellMinded Search[/url]
[url=http://puppylinux.us/psearch.html]PuppyLinux.US Search[/url]

User avatar
Swaphead
Posts: 23
Joined: Thu 04 Nov 2010, 22:36

#47 Post by Swaphead »

This is an interesting discussion.

Many moons ago (the last time I ever did any programming) when Cobol
and PL1 were around I was working in a company that was converting from PL1 to Cobol (mainly because there were more Cobol programmers in the job market)

"GOTO" was frowned upon, and tbh I had no problem with that, but I found
that it was difficult to convert PL1's "RETURN" statement.
An example of this would be ,say, in a validation procedure that would
find the first 10 errors then quit.

The programmer simply coded

IF error_count = 10
THEN return;


after each field was validated.

This was logically equivalent to jumping to the end of the procedure.

In COBOL there was no equivalent (at the time).
A CALL to a PL1 internal procedure became a PERFORM of a Cobol
SECTION.
Every COBOL Section had a labelled EXIT statement to aid readability,
(that was the company standard)
so I would simply convert the RETURN to a GO TO
e.g.
IF error-count = 10
go to 25000-validation-exit
END-IF.


This was my only concession to GOTO, but I don't apologise for it.

Don's tin hat.. :)

User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

#48 Post by mahaju »

So the argument should have actually been against the use of line labels, not the GOTO command specifically (as many other commands also allow, or use line labels)...
But how would a GOTO jump anywhere without line labels?

--- EDIT ---
here's one discussion with some simple examples
http://www.dreamincode.net/forums/topic ... -practice/

User avatar
RetroTechGuy
Posts: 2947
Joined: Tue 15 Dec 2009, 17:20
Location: USA

#49 Post by RetroTechGuy »

mahaju wrote:
So the argument should have actually been against the use of line labels, not the GOTO command specifically (as many other commands also allow, or use line labels)...
But how would a GOTO jump anywhere without line labels?
It wouldn't ( :lol: get the point?... :D :D :D )

Without line labels, you also wouldn't be able to cross do_loops like I wrote. Or be able to use the strange "If(N) 10, 20, 30..."

All of which could be used to produce spaghetti code.

The link that I posted actually railed against the over-use, or misuse of GOTOs... It should have been against the over-use of line labels.
[url=http://murga-linux.com/puppy/viewtopic.php?t=58615]Add swapfile[/url]
[url=http://wellminded.net63.net/]WellMinded Search[/url]
[url=http://puppylinux.us/psearch.html]PuppyLinux.US Search[/url]

User avatar
RetroTechGuy
Posts: 2947
Joined: Tue 15 Dec 2009, 17:20
Location: USA

#50 Post by RetroTechGuy »

mahaju wrote: here's one discussion with some simple examples
http://www.dreamincode.net/forums/topic ... -practice/
Loved the "To infinity and beyond"

But gfortran didn't like it (I think it was written in basic), so here's the repaired code:

Code: Select all

10	goto 40
20	write(*,*)'and'
30	goto 60
40	write(*,*)'To infinity'
50	goto 20
60	write(*,*)'beyond.'
70	goto 10
	End
Of course, I didn't like the formatting, so I fixed it:

Code: Select all

10	goto 40
20	write(*,'(a,$)')'and '
30	goto 60
40	write(*,'(a,$)')'To infinity '
50	goto 20
60	write(*,'(a)')'beyond.'
70	goto 10
	End
:lol: :lol: :lol: :lol: :lol:
[url=http://murga-linux.com/puppy/viewtopic.php?t=58615]Add swapfile[/url]
[url=http://wellminded.net63.net/]WellMinded Search[/url]
[url=http://puppylinux.us/psearch.html]PuppyLinux.US Search[/url]

Post Reply