GOTO lazy programming?

Hey all you handy dandy programming types, why is the use of GOTO sometimes referred to as lazy programming? What are some examples? When did it become lazy programming?

Possibly since this?

Basically when a programmer’s time became more expensive than a computer’s cycles time/hardware

The early programmers took pride in ingenious code. Saving every bit of memory, or millisecond of computing time was a very important skill. Goto was part of that, Don’t bother with easy to track flow, or discretely constructed elements, just go to where you next need to get something done, and get it done as quickly as possible.

But as computers got more and more popular, and faster and cheaper. Readability and maintenance became more and important. You don;t want to bring a contractor in who is going to charge $50+ an hour, then spend the first two weeks diagramming the code flow through all the jumps , then 4 more hacking his changes into every possible random place the flow can proceed from one process to another. You want things very modularized(either procedural routine modularization, or object oriented design) with simply obvious pieces that relate to one purpose, and can be changed independently when the requirements do.

GOTO works well for programs that are less than 10 lines of high-level code. I recommend it.

But 10 lines tends to grow to 12, then 13,000,000 and GOTO becomes a blight on humanity.

http://en.wikipedia.org/wiki/Goto#Criticism_and_decline

goto corresponds to the asm opcode jmp. Did jmp ever get a bad rap?

Nobody* writes assembly anymore. And anyway, assembly code generally does not offer high-level constructs that can replace most usages of goto.

  • Well, practically nobody.

ETA: No pun intended…

As an ex IBM 370 Assembler programmer yes it did. We had very strict rules about where it was to be used, which registers for what segment you could use etc.

And it still made life hell at times.

Otara

:smiley: No problem. I try avoid problems, puns, or other jokes by saying no-one.

I (being nerdy) find that interesting. Thank you. I’ve always wondered about that.

As a kid I first learned how to program in BASIC on the Apple ][e. I wasn’t at all afraid to use GOTO statements then, nor was I afraid to use the very similar GOSUB statement. GOSUB stood for Go Subroutine, once finished a return statement would return the code to the calling line. When I was in High School I learned Pascal, my first procedural langage. This was the first time I was introduced to the idea of keeping subroutines as cohesive as possible (each subroutine should only do one thing) and the idea of trying to keep subroutines as loosely coupled as possible (in other words, each subroutine should be as unaware of other subroutines as possible). Object Oriented Programming took these concepts a step further. My point is, the GOTO statement is the complete antithesis of all of the concepts of software design since at least 1985. It simply makes no sense anymore and if I ever saw it in any software I was reviewing I’d promptly throw that design out and start from scratch.

Do you have an opinion on the COME FROM statement?

Or nobody else needs to know how it works. I’m just yer average computer user, but I’ve made thousands of programs from the C64 on, I know why and where I’m GoingTo and GoSubbing to. It’s not like it’s you just chuck 'em in for fun.

When I was in grad school I taught PDP-11 assembler. The only way we kept sane was to give the class a Pascal program as their first assignment, and grade the hell out of it. (We dropped that grade if it was bad.) The reason for this was to make the class think in terms of higher level constructs - loops, etc. If you did that, your assembly language code became simple to debug and maintain, and you can limit goto use to implement loops etc.

Goto like things like loop exit statements are still required. One of the questions on my oral was designed to trap people who were fanatical goto opponents - I didn’t fall for it.

In any case, they are considered harmful. Dijkstra said it, and I believe it. Though I prefer Knuth’s Computing Surveys article myself.

Remember that BASIC back in the Commodore days was a top down programming only, and you only had two lines to type text on (although using abbreviations for the tokens would let you cheat that). There were very few structural keywords, and although there was IF there was no ELSE (until the C=128). It was a practical impossibility to avoid the use of GOTO back in those days.

Even most modern programming languages have disguised versions of GOTO that are in common use. Typically used for error/exception tracking, you might see a construct (meta language here) like ON ERROR MY_LABEL;, and then at the end of the function, MY_LABEL: handle_exception();

I am not a dogmatic person about goto. It is both quite useful and abusable. But everything in programming is abusable so that’s a canard.

My experience is that less than 1 in 100 times when you think that a goto might be helpful is it actually the best way of doing something. So, yeah, laziness can cause excessive use of goto’s. But it is not proof in and of itself. When it is the right thing, it really helps a lot.

(And remember, it’s not the original coding cost that matters, it’s maintenance that burns the bucks. Intelligent choices need to be made to reduce long term costs. That involves everything. Not just goto’s)

I just think it encourages procedural-based thinking. I used to use them all the time, but now I don’t see the point, except in batch files where it’s pretty much all you have. GOSUB itself was an attempt to move beyond GOTO type thinking.

The sub stands for subroutine, the BASIC name for functions that don’t return a value. You could GOSUB from anywhere in your code, and then come back, just like a function. If you wanted to make it have parameters, set some variables up before going. You want to simulate return values, assign a variable in the subroutine.

I really have a hard time thinking of a case where a GOTO would be the only option. Only in optimization could I see it being useful, and few people optimize anymore.

Dude, you’re making me have flashbacks.

I used to be a COBOL programmer. Back in 1986, I took over a job involving converting and updating some legacy code that originated back in the 60’s. One of the programs I was given not only ended every single paragraph with a GOTO statement, but the program included 7 ALTER statements <shudder>, which change the destination of the GOTOs. I spent several days trying to untangle that SOB before convincing my boss that in the process, I had gained a full understanding of that the program did and we would be better off scrapping and rewriting it from the ground up.

I still write in Fortran '77 sometimes, and goto is necessary in some situations since there’s no CONTINUE or BREAK keywords. But since those are basically the only two places you need it for, as long as you limit yourself to those cases, it doesn’t really cause any problems. If you see goto in code you know its to jump to the end of the loop.

There was always the old DEF FNxx(y), but as a young kid I didn’t really get the grasp for that at the time, and used GOSUB’s as quasi functions like you describe. I don’t know why I couldn’t grasp that; I was programming in assembler, too, for crying out loud.

BASIC 2.0 didn’t have IF-THEN-ELSE (just IF-THEN). BASIC 7.0 introduced ELSE, though. So on the C=64, I’d often have to do stuff like this:



10 IF NM$ = "BALTHISAR" THEN GOTO 60
20 PRINT "YOU'RE NOT BALTHISAR"
30 REM THE NEXT LINE COUNTS NON-BALTHISARS
40 N = N + 1
50 GOTO 100
60 PRINT "YOU ARE BALTHISAR"
70 REM NEXT LINE COUNTS BALTHISARS
80 B = B + 1
100 PRINT N,B

Okay, that’s a little contrived. I think even with ELSE in BASIC 7.0 I had to do that sometimes, because I’m pretty sure the IF-THEN-ELSE all had to be on a single program line (160 characters on the 128).

So was I. Those were good days to be working for a major airline (American).
Younger programmers today can’t fathom writing a program and keeping it within a 1K block. When things advanced far enough for us to get 4K blocks we didn’t know what to do with ourselves.