Subject:
|
Re: Are there GOTO statements in NQC?
|
Newsgroups:
|
lugnet.robotics
|
Date:
|
Thu, 20 Nov 2003 18:20:19 GMT
|
Viewed:
|
945 times
|
| |
| |
In lugnet.robotics, Mark Riley wrote:
> In lugnet.robotics, Vadim Iosifovich Vaynerman <dimav@Glue.umd.edu> wrote:
>
> > I REALLY need to use a goto statement in the NQC programming project I'm
> > doing for the RCX. Dows anybody know if such a statement extsts, and if
> > so, what the syntax is? I know gotos are bad programming practice, but its
> > really hard to make a workaround here. All ideas are appreciated.
>
> I'm probably in the minority here, but I'm going to come right out and say that
> goto's are not a bad thing, contrary to popular belief. In fact, I find it
> rather pompous when I come across admonishments in textbooks that there is no
> valid reason to use a goto statement. This advice presumes that the language in
> question is perfect and can elegantly handle all possible control flow
> structures. Many times, the language is deficient and you find yourself adding
> all sorts of flags and subroutines to come up with a control flow that doesn't
> use a goto statement. Hogwash. There is no shame in it, I say! If you find
> yourself doing these things to make your code "pretty", try using the goto and
> see if your code doesn't become more efficient and easier to read.
As a general rule I do not use goto's because I don't have a need for them.
I've coded so many LOC in my life that the "if then else" structure of modern
language was a step up from where I started. :^)
The original BASIC and FORTAN had gotos and they were necessary (BASIC's "if ()
then LINENO" is nothing more than a conditional GOTO.
One of the places where GOTOs are the handiest in modern languages is in error
detection and recover. They are so important that UNIX provides a special form
of goto called longjmp. You do a setjmp to mark where you want to goto, and use
longjmp when you need to goto there (no matter how many subroutine calls down
the stack you are).
>
> In C, you have the break statement to exit for/while loops. This same statement
> is used to exit switch statements. If you want to exit a for/while loop while
> in a switch statement, you have to play games like adding a flag or messing with
> the condition statement that exits the loop. Oftentimes, this ends up
> needlessly introducing extra code or makes your true intent less readable. It
> also makes your code more prone to maintenance bugs because if you ever alter
> the loop condition you have to alter all the places in your loop where you use
> these special tricks to exit the loop.
Agreed, but I prefer using a flag to exit rather than a goto. I do not look
down on the use of goto, but try to use it sparingly.
>
> Sometimes (in C) you may want to exit an outer loop from an inner loop. Again,
> you can play games with the exit condition (but not always) to find a means to
> exit both loops "gracefully", but oftentimes you create obfuscated code to
> achieve this. You might also try bundling things up in subroutines, but here
> you sacrifice speed for "elegance", which can be especially critical for
> applications in embedded environments. Of course, in this situation you could
> revert to assembly language where goto statements are de rigueur, but why do so
> if you could just as easily use C with a goto statement in that situation.
>
> Now, to be sure, you can misuse the goto statement and I'm sure that's the
> motivation behind the textbook taboo. The logic being that if you don't use it,
> you can't get yourself into trouble. But, my view is that people often cause
> trouble for themselves precisely because they don't use a goto statement where
> it is desirable because of an inadequacy in the HLL language. IMO, the
> textbooks should instruct better on when it is advisable to use a goto, not just
> take the easy route and say "Down that road lies shame!".
Exactly. Cars can be a good thing, but they can also be a killing machine, all
depending on how you use them.
>
> There. Sorry for the rant, but this is one of my pet peeves.
>
> Mark
I enjoyed it. :^)
Kevin
|
|
Message is in Reply To:
| | Re: Are there GOTO statements in NQC?
|
| (...) I'm probably in the minority here, but I'm going to come right out and say that goto's are not a bad thing, contrary to popular belief. In fact, I find it rather pompous when I come across admonishments in textbooks that there is no valid (...) (21 years ago, 20-Nov-03, to lugnet.robotics)
|
19 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|