|
-
Sep 28th, 2009, 06:01 AM
#1
[RESOLVED] What is wrong with GoTo
I've seen a few people on here saying that you shouldnt use GoTo in VB.NET and to be honest I've tended to agree as I have never needed to use it and there is always another way of doing something without using it.
However, I'm just wondering what is so bad about it? I actually noticed that Microsoft seem to use it a fair bit in the internal .NET framework code, which is what makes me wonder why people are so against it.
Cheers
Chris
-
Sep 28th, 2009, 06:48 AM
#2
Re: What is wrong with GoTo
Like so many things, GoTo is not inherently bad but it just has a tendency to get abused. As you say, there are always other ways to do things so why not use them. If you use GoTo then you have to be disciplined to not create spaghetti code. It really doesn't take that much discipline but why require any if you don't need to, plus the fact that some people just don't have any to begin with, so why tempt fate?
-
Sep 28th, 2009, 06:50 AM
#3
Re: What is wrong with GoTo
Hey,
I am certainly by no means an expert on this topic, but my understanding would be that they are a bad practice and should be avoided. This is because you can end up in a situation with "spaghetti" code, where you are jumping all over the place. This results in code that is hard to read and difficult to maintain.
Will be interesting to see what comes out of this thread though.
Gary
-
Sep 28th, 2009, 07:19 AM
#4
Re: What is wrong with GoTo
Thanks for the replies guys, I did think that might be the only reason but just wondered if there was any actual technical reason to avoid using this (performance or otherwise).
So its a good idea to avoid using it purely to keep code tidy and readable right? No other reasons
By the way I'm not planning on actually using it anywhere so I'm not looking for someone to say its ok to use it, I am just curious
-
Sep 28th, 2009, 07:28 AM
#5
Re: What is wrong with GoTo
As JM said, it's not it's bad in and of itself but it does tend to lead to spaggetti code. I was trying to think of why is pastas your code, though, and I think it's because it's uncontrolled. You can send someone anywhere at all in the code with no guarentee of where (if at all) they'll return to. Back in the days of line numbers rather than labels it was really insidious because you couldn't even be sure you'd be sending them to the right place (sooner or later some bright spark would think it was a good idea to renumber your code and the whole thing would collapse in an irrecoverable heap).
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Sep 28th, 2009, 07:52 AM
#6
Re: What is wrong with GoTo
The only place it was justified to use it in was in VB6 error handling. In .NET we have Try Catch error handling and thus no need for GoTo's.
Oh the GoTo xxxx line number technique of BASIC from "back inthe day" Oh the memories.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 28th, 2009, 08:06 AM
#7
Re: What is wrong with GoTo
The only place it was justified to use it in was in VB6 error handling. In .NET we have Try Catch error handling and thus no need for GoTo's
So why is it used quite heavily in the .NET framework itself? (You can see from disassembling the .NET framework DLLs using something like Reflector)
-
Sep 28th, 2009, 08:10 AM
#8
Re: What is wrong with GoTo
 Originally Posted by chris128
So why is it used quite heavily in the .NET framework itself? (You can see from disassembling the .NET framework DLLs using something like Reflector)
I've asked myself the same question as I've seen it many times in Reflector. Maybe they made a policy that GoTo can only be used to jump to labels later in the code, which would avoid spaghetti code. I still don't think it's a good idea though.
-
Sep 28th, 2009, 08:14 AM
#9
Re: What is wrong with GoTo
Yeah I guess that must be the case as thats the only time you see it... oh well, I'll still probably never use it myself but at least now I know why its best to avoid it 
Thanks
-
Sep 28th, 2009, 08:17 AM
#10
Re: [RESOLVED] What is wrong with GoTo
Oh I thought we were talking about user created code and not in the FW.
We cant write the FW code, well we shouldnt unless we get paid by MS lol.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 28th, 2009, 08:21 AM
#11
Re: [RESOLVED] What is wrong with GoTo
Well yeah I was talking about user code but I just mentioned the fact that MS did use GoTo quite a bit in the FW code which is what made me question why I shouldnt use it.
-
Sep 28th, 2009, 08:30 AM
#12
Re: [RESOLVED] What is wrong with GoTo
Well we shouldnt duplicate bad coding practices unless there is a valid reason for it. Im sure MS used it for some good reason, perhaps they wee forced to use it. :shrug:
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 28th, 2009, 08:31 AM
#13
Re: [RESOLVED] What is wrong with GoTo
I find that a bid odd... seeing as how the FW was written in C# ... not VB. Are you looking at actual FW code? OR are you looking at disassembled code? Correct me if I'm wrong, but it could be that Reflector is putting it into "VB" since that is what you are using. I've been through some of the actual FW code, and can't think of a single time it used a GoTo. But then like I mentioned, GoTo is a VB language construct, not C#.
-tg
-
Sep 28th, 2009, 08:34 AM
#14
Re: [RESOLVED] What is wrong with GoTo
Good point TG. If you used reflector in C# what would you see...
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 28th, 2009, 08:46 AM
#15
Re: [RESOLVED] What is wrong with GoTo
 Originally Posted by techgnome
I find that a bid odd... seeing as how the FW was written in C# ... not VB. Are you looking at actual FW code? OR are you looking at disassembled code? Correct me if I'm wrong, but it could be that Reflector is putting it into "VB" since that is what you are using. I've been through some of the actual FW code, and can't think of a single time it used a GoTo. But then like I mentioned, GoTo is a VB language construct, not C#.
-tg
I'm using Reflector to view it but even when setting the language to C# in reflector it still shows GoTo in the same places
MSDN seems to say GoTo is perfectly valid in C# as well... http://msdn.microsoft.com/en-us/library/13940fs2.aspx
-
Sep 28th, 2009, 08:49 AM
#16
Re: [RESOLVED] What is wrong with GoTo
I cant belive they posted that page 
Even look at the posted comment lol.
Maybe they did it to cover their "you know whats"
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 28th, 2009, 08:54 AM
#17
Re: [RESOLVED] What is wrong with GoTo
Like most everything in software development it is a matter of opinion as to whether using a particular construct or feature is a good or bad thing.
GoTo can sometimes be a solid, direct way to control program flow in a simple manner when performing complex gymnastics on if/else clauses would make understanding the code more difficult. OTOH, GoTo when used without any discipline can (as has been alluded to) yield horrible "spaghetti" code.
As always, it is up to the individual developer to set a standard for elegance when they write their code. Goto is no worse or better a feature than anything else - it is simply a tool. In my 35+ years of development I've seen good and bad procedural code, good and bad OO code and everything in between. The language is the language - misuse of the tools is where the problems develop. Poor (or complete lack of) design can produce spaghetti with or without the much maligned GoTo.
To take the dogmatic position that GoTo is either good or bad (whichever "side" you choose) is foolish.
-Max
Last edited by Max Peck; Sep 28th, 2009 at 08:58 AM.
The name's "Peck" .... "Max Peck"
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." - Red Adair
-
Sep 28th, 2009, 09:03 AM
#18
Re: [RESOLVED] What is wrong with GoTo
-
Sep 28th, 2009, 09:06 AM
#19
Re: [RESOLVED] What is wrong with GoTo
Max Peck - I agree but some methods or keywords etc have an alternative that is actually technically better. Some are known to give bad performance or have bugs in or whatever, which is why I was asking about this particular item - to see if there was any technical reason not to use GoTo
-
Sep 28th, 2009, 09:13 AM
#20
Re: [RESOLVED] What is wrong with GoTo
Under the hood, we all use GOTO. After all, it just maps to a jump command, which is a part of every If statement. For that reason, every If statement, and every loop, can be re-written using GOTO. It would probably be possible to enforce some kind of structuring on GOTO targets such that all loop and If statements could be replaced by GOTO. It wouldn't be clear, though, and would take on the opacity of ASM.
My usual boring signature: Nothing
 
-
Sep 28th, 2009, 09:23 AM
#21
Re: [RESOLVED] What is wrong with GoTo
Does it just end up as the JMP assembly instruction when executed by the CLR then? I suppose it does work in the exact same way but I hadnt considered that it just literally 'translates' to that same command
-
Sep 28th, 2009, 09:48 AM
#22
Re: [RESOLVED] What is wrong with GoTo
Is there a technical reason? No. It's probably one of the least costly instructions - a simple jump. However...
Since moving to VB, the number of times that I have found GoTo to be necessary (excepting On Error) ... has been exactly nil.
The problem is that maybe there's that one time out of 1,000 where it is "necessary" ... but it's that 999 other times where it is misused... and BADLY misused that gives it a bad rap. I have the same feelings about On Error Resume Next... to me that's just a horrible thing to do. But others claim otherwise. Meh, what ever boats your float.
My personal opinion and experience is that nothing good can come out of a GoTo and that it should be avoided at all costs. It was a necessity back when there were no such things as procedures... when it was the only way for program control.... that is no longer the case. If it were up to me, I'd have it axed from the lexicon. But that's just me.
-tg
-
Sep 28th, 2009, 09:19 PM
#23
Re: [RESOLVED] What is wrong with GoTo
Yeah, GoTo really is a historical remnant. It was originally a part of pretty much all languages but has really outlived its usefulness. C# is based on C/C++ so it inherits its goto statement from there. The only time it was of genuine benefit in VB.NET that I'm aware of is when you wanted to skip the rest of the current iteration of a loop and go back to the beginning. Now we have the Continue statement but previously you had to use a GoTo with a label at the end of the loop. You could just use If statements but that could lead to some rather deep nesting. C# has had 'continue' from the start I think, so 'goto' was even useful there.
-
Sep 29th, 2009, 09:41 AM
#24
Re: [RESOLVED] What is wrong with GoTo
It's all a matter of degree - GoTo is at one extreme, but note that there are many "slightly sanitized goto's":
Continue For
Continue Do
Continue While
Exit For
Exit Do
Exit While
Exit Select
Return / Exit Sub / Exit Function
Someone expressed surprise at goto being in code generated by reflector for C#, but 'goto' is valid (although not recommended) C# syntax. Note that most languages have 'goto' - the only popular programming language that omits it is Java (it just has the sanitized versions: continue, break, and return).
Last edited by David Anton; Sep 29th, 2009 at 10:10 AM.
-
Sep 29th, 2009, 10:37 AM
#25
Re: [RESOLVED] What is wrong with GoTo
but those are specialized... they have a purpose... and you can't just stick them any ol' place you want. They are part of a larger structure. The destination of GoTos are arbitrary... Continues, Exit and Loop aren't.
-tg
-
Sep 29th, 2009, 11:16 AM
#26
Re: [RESOLVED] What is wrong with GoTo
Technically, you can add If, For, and Do into that list, because each of them causes a jump, as well. In other words, I agree with TG.
My usual boring signature: Nothing
 
-
Sep 30th, 2009, 03:09 AM
#27
Re: [RESOLVED] What is wrong with GoTo
 Originally Posted by techgnome
My personal opinion and experience is that nothing good can come out of a GoTo and that it should be avoided at all costs. It was a necessity back when there were no such things as procedures... when it was the only way for program control.... that is no longer the case. If it were up to me, I'd have it axed from the lexicon. But that's just me.
-tg
Same here
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|