Re:There are valid uses for a GOTO
on
Linux 2.6.17 Released
·
· Score: 5, Informative
1) If you can create a condition where a goto is to be placed, you can add that same condition to the top loop in the nest and let it exit out gracefully.
If that leads to clearer code, then in the cases where you can do that, fine. Do that.
However, there are situations when a condition doesn't make sense until you've already entered the nested loops at least once (for example, when allocating lots of chuncks of memory, you can't test to see if you've successfully allocated memory until after you've tried to allocate memory). Also, if there are several conditions that might require a break, but they can all be handled the same (at least until after you break out of your loops), do you really want each one to be tested at every loop test? Think how big and confusing that would make your continuation test for your outer loops.
2) Use a clean-up function. It will return to the correct place without all the spagetti code.
There's nothing wrong with using cleanup functions if they are convienent for your particular purpose, but if you have to free 11 objects before returning, then you'll need to pass all 11 to the cleanup function each time you call it. I don't know about you, but I usually find functions with 5+ arguments to be ugly. I would rather simply have a 'goto cleanup' that jumps to a label that does all the cleanup in place. An acceptable compromise would be to define a macro that does the cleanup in place but hides it from casual code inspection, thus keeping the code clear, but avoiding the use of GOTO.
Using GOTO in the manners I've described will not lead to speghetti code since the flow of control will be clear and uni-directional (the antithesis of speghetti code). In case (1), the use of GOTO is equivalent to raising an exception in Java, C++, or Python from within the loop and capturing the exception outside the loop (idioms commonly accepted in all three communities). In case (2), the use of GOTO maps multiple exit points to a single exit point. If you feel that these techniques qualify as speghetti code, then I would suggest that you've never seen real speghetti code.
When Djikstra wrote "Goto considered harmful", he was talking about using GOTO to jump outside the scope of the current function, something not possible in with C's goto (C's goto can only jump to a label within the current function). See BASIC and PASCAL (I think) for examples of GOTO that can jump anywhere in the program.
I agree that using GOTO is a bad idea when another control structure is adaquate, but, at least in C, there are times when using GOTO is the most natural and, unequivically, the best choice.
Off the top of my head, I can think of two situations where using a GOTO is the best solution:
1. breaking out of nested loops. In C, the break command can only break out of a single loop level. If you need to break out of 2 or more loops, you can play an ugly game of setting and checking state flags at each level of looping or you can simply create a label at the exit point and use GOTO to get there. (sometimes you can wrap your loops as a function call, but that's often the ugliest solution)
2. shared cleanup code. In a function with multiple exit points, instead of doing cleanup at each exit point, it is often clearer to set your return value and then GOTO a label that handles all cleanup before returning.
Be cautious when using GOTO, but don't be afraid of it. Learn to recognize when GOTO is appropriate and when it should be avoided.
With your mind poisened by primitive imparative only languages, it will cost you 3 or more years in getting fluent in OOP and Design Patterns etc. and it probably also makes it harder for you to understadn UML.
Where do people get this stupid idea that C is the antithesis of object oriented design? People have been using object oriented techniques in C for decades and I've never known anyone who had any trouble picking up object oriented techniques just because their first language was C or pascal.
But if you don't know how to use the hammer appropriately, how can use compare other tools to it?
I mostly agree with the original post: pick a small language (like C) and learn it and its standard library inside and out. Then, as you find domains that are akward for that language, explore other languages that handle that domain better. Find one that you like that, hopefully, will scale to other domains as well, and learn it inside and out. Keep adding tools in this manner when you identify weaknesses in your toolbox, and eventually you'll have the proper tool for any problem you find.
It is my opinion that a programmer who know 2+ languages in depth, will be more likely to pick up a new language and use it well in a reasonable amount of time than a programmer who has shallow knowledge of a dozen languages.
If it's something that I would read anyway, I do it on my own time. If it's something that I would have no interest in except that I need to know it to complete whatever project I'm working on, I read on my employer's time.
I generally run Linux, but I don't purchase a machine until I've verified that it's supported by OpenBSD. I do this for lots of reasons, but the most important is that if OpenBSD, with its uncompromising principles, isn't willing to support it, then neither am I.
I support the hardware makers that support my ideals. Perhaps, if more Linux users did this, there would be more hardware makers that take our ideals into account when making business decisions.
But there is still some upgrading going on. People are aware that their computer is 5 years behind the curve and so when something goes wrong, instead of spending the time and the $50 dollars to replace whatever needs replaceing, people are more likely to spend $300 and get a new (low end) machine which will likely have Vista on it.
That's the how it works. Old machines don't run forever, and the older it is, the more likely it is to be considered "totalled" or "untrustworthy" when something does go wrong (even if it's not a hardware problem).
No, I don't think Blu Ray will kill the PS3. A couple of years from now we're all going to look at the space limitations of the Wii and 360 and wonder why Nintendo and Microsoft didn't think that a HD-DVD format was a good idea.
The PS3 won't always be $600. When the price starts falling, the Wii and 360 won't look like such good deals.
Granted, I'll probably get the Wii 2 years before I get a PS3, but I do expect the PS3 to come into it's own.
No parent is going to pay $600 for a gaming console
Maybe not, but as time goes by, the price will fall.
I think that the PS3 will do just fine. Maybe not right away, but that same technology that makes it initially so expensive will give the console longer legs. 4 years from now, once developers have really learned how to get the most out of the hardware of each of the consoles, the PS3 will shine. And by that time, the price will come down to a more reasonable level.
Or at least that's the reasoning that Sony is using, and it sure seems reasonable to me. Just don't count on me buying a PS3 for the first couple of years it's out.
Usually the startups are starting by individuals graduating from these schools because they've got an idea they want to pursue or would simply rather work for themselves.
None of the articles I read showed a derivation, but starting with the parent post's excellent ball park figuring, we can get an idea how they got there. But first, lets adjust some of the parent's numbers.
First, 50% efficiency for a car engine is crazy. Some 50-ton ship engines approach that (and those use the deisel cycle, not the otto cycle), but car engines are generally reported to be between 15 and 25% thermodynamically efficient.
Second, 80% efficiency of electric motors/generators is severly pessimistic (it's about the efficiency of the hand crank generators from the 50's that you saw in physics class). Even 90% is a little on the pessimistic side for industrial uses, but it's a convenient number to use for estimation purposes.
If we assume a generous 30% efficiency for the engine and a slightly pessimistic 90% for the electric motor, the resulting energy drops from 1637.5 MJ down to 873.3 MJ (242 kWh). At 10 cents per kWh, that brings us to about $24 dollars worth of electricity being equal to 20 gallons of gasoline which is about $1.20 per gallon's worth of electricity.
We've assumed that the electric car weighs the same as the gas car (unlikely) and that the electric car gains no benefit from regenerative breaking (that's actually probably pretty fair since we don't ride our breaks on the highway).
Perhaps someone with more detailed knowledge could give us an idea how optimistic or pessimistic these figures really are.
Current ultra-caps using activated charcoal have about half the energy density of rechargeable batteries. They don't behave like a classical capaciter becuase it takes time for the energy to distribute itself across the surface of the charcoal. Using nanotubes will increase the surface area and energy density, but will probably also require a little more time to charge.
Just for reference, a 2.3 volt, 5000 Farad ultra-cap can take a full charge in about 20 seconds. Running several of these in series does not effect the recharge time (assuming you can supply the required current).
In the last couple of months I've seen several articles that claim that in the case of electric cars, 65 cents of electricity is about equivalent to 1 gallon of gas. So, assuming they're not just making that up, you're off by about a factor of 4. Part of that, I'm sure, is that bulk electricity purchases are generally between 6 and 8 cents per kWh (even less, depending on how predictable your use is).
I'm under the impression that they're dropping the price in order to move chips that they're currently paying to store. They need to flush out the channel before the new chips hit the market otherwise they'll be undersold by their own product.
1) If you can create a condition where a goto is to be placed, you can add that same condition to the top loop in the nest and let it exit out gracefully.
If that leads to clearer code, then in the cases where you can do that, fine. Do that.
However, there are situations when a condition doesn't make sense until you've already
entered the nested loops at least once (for example, when allocating lots of chuncks of memory,
you can't test to see if you've successfully allocated memory until after you've tried to
allocate memory). Also, if there are several conditions that might require a break, but
they can all be handled the same (at least until after you break out of your loops),
do you really want each one to be tested at every loop test? Think how big and confusing that
would make your continuation test for your outer loops.
2) Use a clean-up function. It will return to the correct place without all the spagetti code.
There's nothing wrong with using cleanup functions if they are convienent for your
particular purpose, but if you have to free 11 objects before returning, then you'll
need to pass all 11 to the cleanup function each time you call it. I don't know about
you, but I usually find functions with 5+ arguments to be ugly. I would rather simply have
a 'goto cleanup' that jumps to a label that does all the cleanup in place. An acceptable
compromise would be to define a macro that does the cleanup in place but hides it from casual
code inspection, thus keeping the code clear, but avoiding the use of GOTO.
Using GOTO in the manners I've described will not lead to speghetti code since the flow of control
will be clear and uni-directional (the antithesis of speghetti code). In case (1), the use
of GOTO is equivalent to raising an exception in Java, C++, or Python from within the loop and
capturing the exception outside the loop (idioms commonly accepted in all three communities).
In case (2), the use of GOTO maps multiple exit points to a single exit point. If you feel
that these techniques qualify as speghetti code, then I would suggest that you've never
seen real speghetti code.
When Djikstra wrote "Goto considered harmful", he was talking about using GOTO to jump outside
the scope of the current function, something not possible in with C's goto (C's goto can only
jump to a label within the current function). See BASIC and PASCAL (I think) for examples of GOTO that
can jump anywhere in the program.
I agree that using GOTO is a bad idea when another control structure is adaquate,
but, at least in C, there are times when using GOTO is the most natural and,
unequivically, the best choice.
Off the top of my head, I can think of two situations where using a GOTO is
the best solution:
1. breaking out of nested loops. In C, the break command can only break
out of a single loop level. If you need to break out of 2 or more loops, you
can play an ugly game of setting and checking state flags at each level
of looping or you can simply create a label at the exit point and use
GOTO to get there. (sometimes you can wrap your loops as a function call,
but that's often the ugliest solution)
2. shared cleanup code. In a function with multiple exit points, instead
of doing cleanup at each exit point, it is often clearer to set your
return value and then GOTO a label that handles all cleanup before
returning.
Be cautious when using GOTO, but don't be afraid of it. Learn to
recognize when GOTO is appropriate and when it should be avoided.
With your mind poisened by primitive imparative only languages, it will cost you 3 or more years in getting fluent in OOP and Design Patterns etc. and it probably also makes it harder for you to understadn UML.
Where do people get this stupid idea that C is the antithesis of
object oriented design? People have been using object oriented
techniques in C for decades and I've never known anyone who had any
trouble picking up object oriented techniques just because their
first language was C or pascal.
I wonder how many people report such spam to Google?
But if you don't know how to use the hammer appropriately, how can
use compare other tools to it?
I mostly agree with the original post: pick a small language (like
C) and learn it and its standard library inside and out. Then, as
you find domains that are akward for that language, explore other
languages that handle that domain better. Find one that you like
that, hopefully, will scale to other domains as well, and learn it
inside and out. Keep adding tools in this manner when you identify
weaknesses in your toolbox, and eventually you'll have the proper
tool for any problem you find.
It is my opinion that a programmer who know 2+ languages in depth,
will be more likely to pick up a new language and use it well in
a reasonable amount of time than a programmer who has shallow
knowledge of a dozen languages.
Sequence diagrams are useful for showing things to non-coders.
That's the only thing that I can think of from UML that has value
(and I'm pretty sure I saw sequence diagrams before UML was
created).
If it's something that I would read anyway, I do it on my own time. If it's something that
I would have no interest in except that I need to know it to complete whatever project I'm
working on, I read on my employer's time.
And that's why the gods created stderr.
Send stdout to other programs, send stderr to the human/log.
Simple, yet powerful.
If you go take a look, you'll see that it's back.
I generally run Linux, but I don't purchase a machine until I've verified
that it's supported by OpenBSD. I do this for lots of reasons, but the
most important is that if OpenBSD, with its uncompromising principles, isn't
willing to support it, then neither am I.
I support the hardware makers that support my ideals. Perhaps, if more
Linux users did this, there would be more hardware makers that take our
ideals into account when making business decisions.
The inability to recognize the true cost of compromise in
a given situation is definately a weakness.
But there is still some upgrading going on. People are aware that their computer is
5 years behind the curve and so when something goes wrong, instead of spending the
time and the $50 dollars to replace whatever needs replaceing, people are more likely
to spend $300 and get a new (low end) machine which will likely have Vista on it.
That's the how it works. Old machines don't run forever, and the older it is, the more
likely it is to be considered "totalled" or "untrustworthy" when something does go
wrong (even if it's not a hardware problem).
No, I don't think Blu Ray will kill the PS3. A couple of years from now
we're all going to look at the space limitations of the Wii and 360 and
wonder why Nintendo and Microsoft didn't think that a HD-DVD format was
a good idea.
The PS3 won't always be $600. When the price starts falling, the Wii
and 360 won't look like such good deals.
Granted, I'll probably get the Wii 2 years before I get a PS3, but I do
expect the PS3 to come into it's own.
No parent is going to pay $600 for a gaming console
Maybe not, but as time goes by, the price will fall.
I think that the PS3 will do just fine. Maybe not right away, but
that same technology that makes it initially so expensive will give
the console longer legs. 4 years from now, once developers have
really learned how to get the most out of the hardware of each of
the consoles, the PS3 will shine. And by that time, the price will
come down to a more reasonable level.
Or at least that's the reasoning that Sony is using, and it sure
seems reasonable to me. Just don't count on me buying a PS3 for
the first couple of years it's out.
Could you please expound on that idea a bit?
Usually the startups are starting by individuals graduating from these schools because they've got an idea they want to pursue or would simply rather work for themselves.
How many different colors does the Nintendo DS and DS Lite come in?
Funny you should say that considering how much better the iPod is doing in
Japan than any of its Japanese-manufactured competitors.
None of the articles I read showed a derivation, but starting with
the parent post's excellent ball park figuring, we can get an idea
how they got there. But first, lets adjust some of the parent's
numbers.
First, 50% efficiency for a car engine is crazy. Some 50-ton ship engines
approach that (and those use the deisel cycle, not the otto cycle),
but car engines are generally reported to be between
15 and 25% thermodynamically efficient.
Second, 80% efficiency of electric motors/generators is severly
pessimistic (it's about the efficiency of the hand crank generators
from the 50's that you saw in physics class). Even 90% is a little
on the pessimistic side for industrial uses, but it's a convenient
number to use for estimation purposes.
If we assume a generous 30% efficiency for the engine and a slightly
pessimistic 90% for the electric motor, the resulting energy drops
from 1637.5 MJ down to 873.3 MJ (242 kWh). At 10 cents per kWh, that
brings us to about $24 dollars worth of electricity being equal to
20 gallons of gasoline which is about $1.20 per gallon's worth of
electricity.
We've assumed that the electric car weighs the same as the gas
car (unlikely) and that the electric car gains no benefit from
regenerative breaking (that's actually probably pretty fair since
we don't ride our breaks on the highway).
Perhaps someone with more detailed knowledge could give us an
idea how optimistic or pessimistic these figures really are.
Current ultra-caps using activated charcoal have about half the energy density of rechargeable batteries. They don't behave like a classical capaciter becuase it takes time for the energy to distribute itself across the surface of the charcoal. Using nanotubes will increase the surface area and energy density, but will probably also require a little more time to charge.
Just for reference, a 2.3 volt, 5000 Farad ultra-cap can take a full charge in about 20 seconds. Running several of these in series does not effect the recharge time (assuming you
can supply the required current).
In the last couple of months I've seen several articles that claim that in the case of electric cars, 65 cents of electricity is about equivalent to 1 gallon of gas. So, assuming they're not just making that up, you're off by about a factor of 4. Part of that, I'm sure, is that bulk electricity purchases are generally between 6 and 8 cents per kWh (even less, depending on how predictable your use is).
I'm under the impression that they're dropping the price in order to move
chips that they're currently paying to store. They need to flush out the
channel before the new chips hit the market otherwise they'll be undersold
by their own product.
I'm sure that both Sun and IBM would disagree with you.
What would it cost to ship from Africa to the Americas or Europe?
Don't keep it in your car. High temperatures degrade Li-ion batteries.