- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Nothing wrong with responsible use of GoTo in VB6, e.g.
I've even named the label "Catch" to for all the cough 'real' programmers ;-)
Admin
It's not a documented bug, but if you do a bunch of them and compare to a program that is coded to do the same thing but without the GOTO's, you'll see the speed difference as the GOTO's bog down your system. It's probably not an issue with VB.Net, but having used the other for many years I can attest to having seen this a number of times.
Addendum (2007-02-23 11:14): References to the compiler and other weird issues in VB's GOTO: http://forums.devshed.com/visual-basic-programming-52/is-goto-safe-416576.html http://visualbasic.about.com/od/imhoinmyhumbleopinion/a/GraGoTo01.htm
Admin
More than 90% of people who cite that Dijkstra's article never read past the title.
From one of Dijkstra's manuscripts (http://www.cs.utexas.edu/users/EWD/transcriptions/EWD13xx/EWD1308.html):
Admin
devo r00ls!
Admin
We code in C and to hide the fact that they used gotos, some developer ages ago decided to do this deep in a common header file: #define _throw goto
So we would have to write this code something like this:
#define _else goto
skipIt:
Admin
;)
Admin
Nothing wrong with gotos innit.
Admin
Just as long as that job isn't maintainer of the Linux driver for the Sun Happy Meal Ethernet adapter. Excerpt:
Admin
I know that C# is not the same as C or C++, but VB.NET is almost exactly like C#.NET.
The same keywords, the same constructs, the same capabilities, and often the same intermediate-level code gets generated as an equivalent C# program would generate. Which means it runs as fast as the same code written in C#.
VB.NET has try/Catch/Finally; it has collections and the ForEach statement; it has inheritance and base classes and derived classes; you can create your own constructors and destructors in your base and derived classes; it handles events; it has interfaces and Implements; it has Public/Protected/Private/Shared/Friend/Static; it has overloads; it has CreateObject; you can make a nonvirtual call to a virtual method of the current object; you can declare a partial definition of a class or structure; it has delegates; and it has threading primitives.
VB.NET is not a "toy" language nor a "stupid" language.
Admin
I wish I didn't see this all the time in some code I'm "updating" at work, but sad to say I do. To make it worse, the code I'm working on is a bizarre mix of C and C++! Yuck!
Admin
It's things like that, that make me wish c/++ had anonymous functions available.
At any rate, seems like you probably could have wrapped up those goto blocks in small sub-functions, and just called the sub-functions from the case statements....
note: even w/o function pointers you could do:
struct Pos { int x, int y }; struct MoveCmd { int key, Pos delta }; MoveCmd MoveCmds { { UP, { 0, 1} } , { DOWN, {0, -1 }} , { LEFT, { -1,0 } }, { RIGHT, {1, 0 }} }; struct ZoomCmd { int key, int z }; ZoomCmd zoomcmds { { ZOOM, -1 }, { SHRINK, 1 } };
bool handled=false;
for ( i=0; i< sizeof(movecmds); ++it ) { MoveCmd* cmd= movecmds+i; if ( handled= (key == cmd->key)) { bool ok = testIfOutOfBounds(cmd->delta); if (ok) { // move . . . } break; } } if (!handled) { // similar zoom code . . . } assert(handled); if (!handled) { // error . . . };
which would easily allow you to add diagonal keys, undo, redo tracking, etc. =>
Admin
Are you kidding? You almost got me. Had this been slashdot, you would have been flamed...now.
I swear - I typed that then looked at my CAPTCHA: slashbot. What a freaking weird coincidence.
Admin
That comment was directed at Alky. I thought it automatically quoted when replying...
Admin
Only if you use the "Quote" button, instead of the "Reply" button.
Admin
non-LIFO -- FIFO FILO PUFO
PUFO is for gotos. Pick up and F*ck Off
Admin
3 of the 4 screw-ups that the submitter talks about are one and the same (not using else = logical flow issues = reduced legibility)
the remaining one is a non-issue - abnormal exit, wtf? memory/compiler issues, wtf? in what kind of interpreter/compiler are there issues with jumping out of an if block? does the submitter actually think this code would ever run in any other interpreter except what it was coded for? ok, well, to be fair perhaps it might some day run on the next version of the same interpreter, but it is quite certain that there will be no problems with that.
granted, the snippet is a wtf. the coder obviously wasn't familiar with such VB basics like the else statement. he probably had experience with other languages, i.e. assembler or, for what it's worth, the original BASIC language where there is no construct like else and a similar use of jump/goto is in fact the proper way to code this.
but the submitter's comments are even a bigger wtf in the way they're trying to present this as more than it actually is. can't figure out "where we skipped here from"? gee, a search for that label is too hard... perhaps you'd have preffered a "comefrom" statement to clarify things for you?
Admin
Admin
Yep, so I am currently trying to learn OCaml...
Yes, but it means that I write a sub-function for three-four instructions. These subfunctions have the disadvantage that they must be repeated in each case and must be followed by a break instead of a goto. They are awfully small and they are separated from the main code.
You mean data pointers ? (I don't see a function) No offense meant, but I think the pointer solution is inferior in this particular case:
a) You must allocate several structures to mimic a simple assignment.
b) I mentioned that there more than two cases in the routine ; in the example I wrote they are indicated by ... So you can't use an if; you must use either a switch or if/elseif - cascade.
c) The main code remains in one place in the switch-goto solution in contrast to the structs and pointers.
There is simply no reason in this particular case to use not this solution; if the blocks were bigger or the number of cases higher (>30) I admit your solution would be preferable. The goto hate is quite irrational; since four years I have needed it only two times, but when I need it, it is indispensable.
Admin
I see nothing wrong with that... All ZX Spectrum program were written using GOTOs. In those days we didn't have all this fancy stuff like "else" keywords. Those are for pussies!
Catcha: dubya - not again!!!!
Admin
What?
As opposed to gotos, where it's not at all clear where you're coming from and where you're going to?
Admin
Obviously the writer was an old Fortran IV programmer that had never really accepted this new-fangled block programming.
The only target for an IF was a single statement, so you usually used a GOTO. The following line was therefore the ELSE and you would have to search the code to find the label to THEN. Of course both of those 'blocks' would terminate with another GOTO, hopefully further down but in a WTF program could well be somewhere in the series of statements being executed in the branch.
Admin
-Harrow.
Admin
Most developers don't understand how many stupid VB developers there are. Ease of syntax has allowed the barbarians in the gate. I used to work for a company where the product's central business-critical task was all done within the click event of a button. The code was 150 printed pages with enormous "IF" blocks. Everything was indented just 1 or 2 spaces. It was full of GOTOs, arbitrarily named variables, multi-dimensional arrays with cryptic names (e.g. If q(3,7,2) = "a" then goto sch_cal_1). Although the "core" development team (core meaning not smart enough to get a job elsewhere) had been there 3-5 years, one of them didn't even own a PC at home. None of them even knew how to create a dll, much less understood when/why to use them. Both claimed to have degrees from universities in far distant lands --- one even claimed to have a masters in mathematics. The surest way to get hired was to say "I'll do the job for $50K" when the position was advertised at $65K. And the "got the company from my mommy" CEO couldn't figure out why his product sucked so bad --- stupid beeyoch!!!
Admin
I'm not sure what evil breed of BASIC that's coming from, but it reminded me of the ON ... GOTO ... instruction in BASICA or GW-BASIC.
Neither BASICA nor GW-BASIC allow the statment you've quoted. That is, this is not a valid BASICA program:
Instead, you would do this:
Admin
I might be wrong, but I thought that VB was the primary language used to write ASPs. As such, I don't understand this sentence.
Admin
Ack. Sorry about the odd "VAL(X$)"... here's the syntax-error-free version:
Admin
it does in TI-BASIC, though that's a different language.
considering you don't have functions or (working) subroutines, that forces you to use single-statement-ifs-plus-gotos all over the place.
Admin
Meh. I used to see this sort of crap all the time. I used to work with a poser VB programmer, and his coding was terrible. If it was as bad as this, I would've been an improvement.
His next language was ColdFusion, and the clusterf*cks he did in that platform was akin the killing fields of Vietnam, such was the horror.
Admin
do
loop, really). Tom Duff was also slightly dubious about the construct, so he asked Dennis Ritchie if this was legal C who endorsed it.And the best thing is that thanks to Google Groups you can actually read the original post
Admin
If you're going to want people on TDWTF to explain something, it helps to get it right.
CAPTCHA: gotcha
Admin
Please tell me this is some kind of elaborate joke that I don't get. Otherwise, please learn basic C syntax before you try to criticize others.
Admin
As Marshall Cline says "Goto's are evil."
I have only used one once, and that was due to a poor design decision in the beginning. Not my decision, but one I was constrained to. I coded it, and then left before I got fired for the results.
Admin
Not exactly. ASP (primarily) uses VBScript, which is a dumbed-down version of pre-.NET VB. VBScript is such an atrocity it makes VB6 look GOOD.
Admin
That's the same as saying that every if/then/else construct is actually a "goto". Technically, at the machine level, that's what it looks like, but the point of a high-level language is that we really don't care what the low-level instructions look like.
Admin
I think you're missing the point. Under the surface, a function call is also a goto. Actually, any flow-control construct is a goto, in a way. Neverthelsss, modern programming languages have provided high-level constructs that take the label [or line-number] tracing out of the equation and allowed for logical flow.
The commands break, continue, next, and Perl's last can all certainly be misused, as any programming construct can. they can also be put to very good use. Generally, good compilers will issue a warning if these are used in predictably perverse ways.
Explicit gotos are relics of a prior century, and should be relegated to the Museum of Obsolete Technology.
Admin
Feel free to read Dijkstra's article in its entirety. Please, for the sake of the sanity of maintenance programmers worldwide.
The article was first published almost 40 years ago in 1968, and people are still using GOTO? Dijkstra must be rolling in his grave.
Admin
Admin
Most people would call those 'comments', generally most languages have them available...
Admin
There are still PLENTY of uses of goto that are quite reasonable. It's used all over OS kernels for instance to do error handling because there alternatives are worse.
Here is it with gotos:
Here it is without gotos:
Which is more readable? It's probably a style choice and not a clear win, but option #2 seems to be preferred by most people.
I have another use for goto that I'm doing right now that I don't want to get into that would work similarily.
Admin
What?
Generally comments don't make me go "I wonder how this affects the program's execution"...
Admin
The main issue is that there is no real reason to use goto in VB6. In fact, there's no reason to have goto in VB6. It's there because the original MS BASIC had goto.
Admin
pah, using goto is fine if it solves the problem at hand. Its hardly a wtf realy. Sure if your starting from scratch you should avoid bad technique like this, But when the business wants that change now now now! (theres all ways a big deal to close if the colour can just be pink) then you have to do what you can to make it work with minimun change to the existing code. Not go off on a rant about you will have to refactor the entire code base to your fave OO methodolgy.
The customer wants fries Now! not made in an easier to maintain fryer next week. I've made the sale and you guys are spending money on a new fryer no-one wants.
Admin
I've worked with morons like you that just spit out code that works like crap. And know what? I finish the stuff I write that's actually maintainable BEFORE they do. It's amazing how they trip over themselves fixing code they can't even comprehend that they wrote the day before. Yeesh.
Admin
This is not a post condoning the use of goto's in this case, but if the VB compiler is any good then the use of a goto here shouldn't cause memory or compiler issues.
For those who remember your compiler class and can recall how to create basic block graphs, give it a try for this example. You'll notice the difference between the if-else graph and a if-skipit graph is a change of one node's follow arrow to a branch arrow. A good compiler can cope with this situation without using extra memory or getting stuck in some difficult graph optimization algorithm.
Now, if the compiler for VB 6 or VB.Net is any good is a different issue entirely :-). Anyone willing to test and benchmark them?
Admin
And gasp it's possible to write useful, easy-to-maintain code utilizing the benefits of GOTO.
If you comment your code, then whatever solution can be easy to maintain. It's true that mixing GOTO with an OO solution probably isn't a good mix. But if you're dealing with primarly iterative code, it can actually be easier to understand with GOTO and descriptive labels than we code blocks. One can argue for functions, but how much different is a function from a GOTO anyway?
So long as the code that freeShipping and doAddShipping refers to jump to the same place at the end, there's not much difference. And when you get down there and you see:
These aren't inherently horrible uses. More modern structures are more natural for many situations (including my example), but occasionally GOTOs work even better. Especially in languages that do not allow multiple function return points; using GOTO you can simulate that behavior.
Admin
AHAHAHA!!! Another one who didn't get it!! :)))
Captcha: craaazy
Admin
yeah yeah, talk to the hand girl friend! I know your beardy coding style, you'll spend years working out the perfect system that can never go wrong and then throw a linux fit when a real business need comes along that you haven't thought of because you'll have to change your precious model to fit it in.
Then you'll make up stuff like 'maintainablity', 'documentation' and 'testing' to justify why the guy next doors 2min fix isn't good enough when the customer is happy and shelling out for it!
At the end of the day money talks, make it work the simplest and quickest way you can, refactor later if nessecary.
Admin
Um... "maintainable code" is a different issue than "perfect system".
His point is that maintainable code can be changed faster than unmaintainable code (which results from unrestricted GOTO).
For the record, there's only one situation where you need GOTO: leaving nested loops immediately. And even then the language could make it unnecessary:
For y = 0 to 15 do For x = 2 to 57 do If (a <> b) then Break(y);
Admin
I've yet to see an example that is impossible to not use goto.
Even jumping out of two nested loops.
for(int x = 0;x < 100;++x) for(int y =0;y < 200;++y) if ((x == 50) && (y == 50)){ //Some code goto skipIt } skipIt: // Some code
Can be refactored into this
void Find5050(){ for(int x = 0;x < 100;++x) for(int y =0;y < 200;++y) if ((x == 50) && (y == 50)){ //Some code return; } }
...
Find5050(); // some code
If someone can come up with an example, I would like to know.
Admin
In some cases, a judiciously placed goto can be by far the cleanest method of exiting from a set of nested if ... then structures. Instead, second rate programmers will often make their code nearly unreadable trying to avoid using a goto.
Another good use is as a common exit point from a function. For example, should you have a number of open files to close and more than one return, you have to close all the open files at each return. Instead, a simple goto to a common set of code and you only have one place to close all the files. It is not only cleaner, but it is more understandable as well.
Sure, gotos can easily be abused. But refusing to use them at all because you think they are somehow uncool is nothing but another form of self abuse.