![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Crazy Challenge =D
OK, here's a challenge for you uber-nerds, and you're not gonna like it.
Write a program/algorithm in a high(er)-level language that uses goto. Bad enough, you say? Here's the killer: the algorithm must not be able to be more elegantly written using higher-level constructs such as loops, comparisons, etc. It has to do something useful - you can't just have twenty nested loops and break out of them with a goto for no reason. Ready? Go! |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
There are few, if any, reasons to use a goto in normal code, aside from deeply-nested constructs, which you have outlawed as a suitable reason. Nevertheless, there are things that can happen in a system that represent fatal conditions. Some of these can be detected by the OS, presuming you have more than a shadow of one. Some cannot. In some instances, an interrupt routine cannot even return without regenerating or compounding the error. One might overwrite the return address and perform a jump in that way, or one might just use a goto. I would say that the most prevalent VALID use is in the handling of such errors. Error handling with try/catch blocks has the benefit of cleaning up a number of things, presuming that the language is written properly, that one would have to take care of overtly in using a simple "goto" to a specific central location from any number of possible failure sites. There isn't any less thinking or work, of course, you just try to put it off on the "other guy".
My typical use of a goto (and they're damned infrequent over the course of many years of programming) IS to exit the nested-loop situation when the exit is not caused by the logical progression of code that entered the loop to begin with, but by the occurrence of an unrelated, real-time event, that dictates getting out NOW, without recourse to following a crumb-trail of flags especially designated for that purpose.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Basically, my aim here was to see whether goto is actually a useful keyword to have at all. Thanks for the response, my friend.
|
|
|
|
|
|
#4 |
|
Programmer
|
I guess the only time I really use goto is in batch, so..
/me shrugs shoulders. |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Yes, it has its uses. Unfortunately, it's a temptation to laziness, or a symptom of limited vision. Unless the language restricts its use to a limited scope, it can lead to absolutely unfathomable code for those that run upon it cold. The fact remains that, ultimately, all non-contiguous paths of executions are implemented at the machine-code level by a dead jump, a "goto", conditional or otherwise. I'm looking forward to seeing the responses you get.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Programming Guru
![]() |
Does TI-Basic count as a higher-level language, i mean it's not ASM. Ok, well, probably not but that's one of the few languages in which i know there is a necessity for GoTo.
__________________
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|