- 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
Perhaps
Admin
"hmmm... wonder where we skipped here from?"
This is, of course, why INTERCAL's COME FROM statement is so useful.
removes tongue from cheek
Admin
These days I have to rely on enormous, end-user-created Excel spreadsheet "databases" to satiate my appetite for variables with no relevant naming and heaps of barely-traceable goto statements.
</sarcasm>
Admin
Wait, is Visual Basic really so crap that goto-ing out of an if-block can cause some kind of leak? Or is the poster just talking out of his ass?
Admin
Here's what happened.
The code was originally written without the if-statement and the SkipIt label. They suddenly had a need for a very specific case that they needed to take into account. Some old fart assembly programmer didn't know how to write a proper if/else statement but he remembered gotos. So he added the new condition and goto to skip the original code.
Admin
I thought most languages separated gotos and conditionals as much as possible. Just think if you jumped into the middle of a while loop or an if-statement. Most languages find it just as offensive to jump out.
Admin
It seems to me, from reading these forums, that visual basic is all OVER the place. Or perhaps more stupid code is created for visual basic. That'd be an interesting question to get some stats on...
Captcha: Kungfu
Admin
the latter
Admin
When a problem comes along. You must skip it. If the code becomes too long. You must skip it. When something's going wrong. You must skip it.
try to detect it. it's not too late. to skip it. skip it good.
Admin
No. In principle you have enough goto's in every current language. "break" "continue" (also called "next" sometimes) and don't forget "return" which is often used to jump out of a subroutine.
Don't get me wrong. I don't hate those, but in principle those are also goto's.
Admin
Admin
This is nothing! Here's what I deal with on a regular basis.
And that's when the sub is not over 800 lines long.
Admin
I don't know any VB. Does the compiler provide any warnings like C/C++?
Admin
Programmer who uses a goto construts must burn in Hell.
Admin
Captcha: RIAA. They're everywhere damnit!
Admin
No. Not like C++. You mean the warning of multiple exit points etc. No nothing like that all.
Admin
Visual Basic is very clear about when and where you can use goto:
Jumping out of an if cannot be a problem for VB because otherwise the standard (olde worlde) practice of: would be a problemAdmin
Maybe it's just a dialect / accent problem. One programmer wannabe asked the other how to solve a string matching task. The other just replied: "You've got to escape it". Not eager to ask anymore questions, the lamer thought he understood it as "You've Goto SkipIt". After inserting this line it broke the code even worse. Finally he found the solution: he forgot to use the label "SkipIt". And it worked.
Captcha: kungu is being spelled in many dialects differently
Admin
LOL! That is definitely going on my huge whiteboard.
Admin
Or:
If a problem comes along, Go to skip it?
Admin
You're talking about VB.NET which is a very different language all together. Try Catch does not exist in VB. I just slapped this together in VB 6.0 and it ran just fine.
Edit: Ooooo yuck! This is the most hideous code I have ever writen. Oh my God this is gruesome.
Admin
I've got this in my vb code. I suppose its lazy but it gets the job done. The alternatives would be complicated nested if blocks, moving existing code out into new functions or having status varibles which you check ie. "if condition occurs status = 'this condition has occured'"
at the end of the day if you can solve a problem in 5min with 'bad code' its difficult to justify to the business why you should spend a day or two rewriting existing code a better way and probably introducing more errors instead.
Admin
Thousand-line functions? Meh. Some of the more monolithic modules I've worked on have been far exceeding that, though they were mostly put into functions for cosmetic reasons only anyhow -- it was just something like:
DoThing1(); DoThing2(); DoThing3(); DoThing4();
You could break them up further so as to bring the LOC count down for each individual function, but it still would've had more or less the same end result.
Admin
Both are true. The main thing about VB is that it has fewer "barriers to entry" than most other languages, meaning that it is easier to start using the language if you have no programming experience. This basic aspect of the language meant that a lot more people started using it in the 90s (hence VB code's ubiquity), and a lot more people used it badly (hence the high level of VB code's crapocity).
Admin
Simon Tatham's coroutine macros (in C) uses a switch to jump into a for loop. http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
This trickery is, as far as I understand, used in Putty to implement the SSH protocol.
Admin
Yay, a Devo parody!
Admin
The trouble with VB is that it's easy for an idiot to produce 'something' and call himself a programmer, the problem is compounded when the idiot gets employed by a naive/ignorant company to write production code !
Admin
when people speak of goto, they usually are thinking of being able to jump to any place in the code. If that was possible, then yes, it could lead to leaks, logic errors segmentation faults and even the end of civilisation as we know it. This is not quite the case however (see below, though I admit I don't know VB so I only assume what is said about it).
Return, break and continue aren't goto statements because it's fairly clear where you're coming from and where you're going to. This is an important distinction.
So basically VB goto isn't the goto we all (should) avoid. Why they would then even use that name is beyond me...
E. Dijkstra wrote a short article about the classic goto, and why it is considerd harmful. This is of course about the classic goto statement which allowed you to jump to any arbitrary point in the program. If you don't understand why this is harmfull you are either a beginning student or unsuited to the IT profesion.
Admin
That is because you can not think past your next nap. You are lazy. When you use GoTos in your code, your code becomes a little more difficult to maintain. That function will not stay the way it's written today forever. It's only a matter of time before someone else (or you) has to come in and modify the GoTo code. Five or six edits later with 8 or 9 extra GoTos will make the code bulky and eventually very buggy.
Most of a programmers time is spent maintaining code. Therefore it would make since to write your code so that it is easy to maintain. The 5 minutes that you save today may take someone an additional 10 minutes if they want to add on to your functions at a later date. This may in turn take 20 minutes for someone else down the road.
There have been many smarter and more experianced people before us that preach to us to not write code "the bad way" for a reason. That's because rather then at the end of the day, but at the end of the life cycle of the application, good written code can be changed much faster then bad written code.
PS: Sorry about the lazy comment. I'm gettting a chance to blow off steam. I don't think you're lazy...I don't even know you. I doubt someone who admits that they might be lazy really is lazy.
Admin
I'm still waiting for someone to explain this construct (found in K&R, even):
if(testFailed) return A; else return B;
I reaaally don't understand how protecting a return with an else is useful.
Admin
Ok, I am outing myself:
Once upon a time I coded in C++. I had to intercept key/menu commands which were interrelated (like direction commands of the up/down/left/right key); I will call them blocks. There were several such blocks in the code. In all there were 5 blocks of 3-5 commands (~20 commands) with 1-2 lines setting the variables and then doing always the same routine (3-4 lines) in the block. I wasn't interested in function pointers because it felt like breaking a fly on the wheel. So I did:
int x = 0, y = 0, z = 0;
switch(key) { //--------- BLOCK1 (moving) -------------- case UP: y = 1; goto block1: case DOWN: y = -1; goto block1: case LEFT: x = -1; goto block1: case RIGHT: x = 1; // ------- BEGIN BLOCK 1 routine ------------- block1: bool ok = testIfOutOfBounds(x,y); if (ok) { move(x,y); protocolXY(x,y); } break;
//-------- BLOCK2 (zooming) ---------------- case ZOOM: z = -1; goto block2: case SHRINK: z = 1; // ------- BEGIN BLOCK 1 routine ------------- block2: bool ok = testObjectDistance(z, incDistance); if (ok) zoom(z, incDistance); break; .... default: assert(false); throw COMMAND_HANDLER_ERROR; }
And I am still convinced that it ISN'T bad....
Admin
As already intimated, the solution to this is the famous COME FROM statement, as described by R. Lawrence Clark in his paper of 1973, which I'm sure you have all read. (It was required reading for that test you passed.)
More seriously, VB (and VBA) are perfectly good languages, without most of the faults of C such as uncontrolled use of pointers and IO, string and array operations that can write beyond the end of buffers - though of course it has new faults of its own, plus a few nasties inherited from its earliest days that no sane programmer uses any more.
It is quite easy for a competent programmer to write clear, readable and even efficient code in VB. I think the reason people see so much bad VB is that it (or rather VBA) is the first language non-programmers try when they're trying to do something in Excel and get told "just write a macro". After which they think they are programmers.
Self-taught programmers are like self-taught drivers: even when the resulting crashes are spectacular, they will be quite sure that it's not their fault :-)
For those with poor memories or who suffer from insufficient age, read it at http://www.fortranlib.com/gotoless.htm and weep...
Admin
Maybe they came from assembly language programming that doesn't have if/then/else constructs. E.g.
etc.Admin
Programmer who uses a goto construts must burn in Hell.
Not necessarily. If you don't have exceptions available then goto is a perfectly acceptable way of handling errors:
is nicer IMO than:
Admin
Addendum: The code was formatted; the forum software has deleted the whitespace.
Admin
If you can't find a better way to do it, you probably shouldn't be doing it at all. It's not 'bad code' because of a style issue, or some type of fad. It's bad because it introduces more errors. That kind of programming might seem like a good idea but it isn't. There is always a better way of doing it which is less error prone. What justifies it is the cost of hiring someone later on to go in, figure out what happened and fix it. That's usually much more expensive.
Admin
Admin
It's explicit conditioning for legibility. We know that if the 'if(testFailed)' is false that 'return B;' will be exercised, but the 'else' makes it more easily read.
CT: pirates... aaaarrrrg
Admin
If it takes you a day or two to write an if-else statement, you really don't deserve to be employed anywhere unless it involves the phrase "Do you want fries with that?"
(Captcha: "ewww". My thoughts exactly.
Admin
Admin
I vaguely remember my basic programming days... Some OS360 hotshot programmer tried to explain to me how you should do stuff like this:
and his code was riddled with stuff like this.
Admin
I tend to do that because it makes the code structure more obvious, and if someone ever changes the code in the first block (return A) so that it doesn't return, it avoids a potential bug.
Admin
Here's a nice example from Microsoft Look at the examples for great VB.Net and C# versions of a goto within an IF within a switch/select... http://samples.gotdotnet.com/quickstart/howto/doc/Xml%5CMultipleXmlReader.aspx
(To see the C# version, goto (ha!) http://samples.gotdotnet.com/quickstart/howto/ and use the dropdown for language)
Admin
Customer: I'd like a Big Mac (tm) Putz: Do you want fries with that? Customer: No thanks Putz: gosub processOrder '... ' sub: processOrder process burger part of order goto fries ;; no fries
Admin
Me, I'm a fan of including a few goto labels at random in the code. No gotos, of course, just the labels. Even though they're completely meaningless, I know there's a pleasing pulse of oh-geez-what-does-this-do coming up the next time someone looks at the code...
Admin
Because everything seems so "familiar" and semantic, people feel they know what this or that is for, and don't bother to actually learn the language.
So we get a bunch of idiot programmers with two years experience programming when all they've been making is stuff like that.
Languages like Java, PHP, C, or Perl tend to scare away the novices. Try-catch blocks? What are these squiggly parenthesis? What the hell is a "float"? Why are there so many exclamation points? What-masking? So you either get moderate code, or those who do try to program in it shortly prove that they shouldn't be.
This is why I recommend that all HR departments request code examples from applicants, then have the development department looking over it. It's easy to see that the development environment I have doesn't do such a thing.
Admin
This fact should be pounded into all VB programmers' heads. Don't use goto. Just don't. Hell, avoid On error goto as much as you can. If you must use it, mimic a try block like so:
Admin
Is "Alkari" code name for Lucifer because you must be the devil!
Admin
Actually, most stupid code comes from Visual Basic ... because Visual Basic itself is a stupid language.
M$ screwed up generations of developers by nudging these atrocities, and then try to fix it with C#. Too bad they threw in VB.net, enabling awful code to remain in their shiny-new enterprisey platform.
I wish I still had one of my former collegemate's source code for a "non-LIFO" stack in VB ... go figure what that was used for.
Admin
well, I thin we have to excuse assembly programmers, it would make life difficult otherwise. Of course, there isn't all that much need to program assembly anymore...