473,732 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is wrong with the goto command?

Can anyone tell me what is wrong with the goto command. I noticed it is one
of those NEVER USE.

I can understand that it may lead to confusing code, but I often use it like
this:

is this wrong?????

Function x
select case z
case 1
goto one
case 2
goto two
case else
goto three
end select
exitFunction:
exit funtion
one:
.....
goto exitFunction
two:
............... ..
goto exitFunction
three:
............... .
goto exitFunction
end function
---
Please immediately let us know (by phone or return email) if (a) this email
contains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.698 / Virus Database: 455 - Release Date: 02/06/2004
Nov 13 '05 #1
51 13377
WindAndWaves wrote:
Can anyone tell me what is wrong with the goto command. I noticed it is one
of those NEVER USE.

I can understand that it may lead to confusing code, but I often use it like
this:

is this wrong?????

Function x
select case z
case 1
goto one
case 2
goto two
case else
goto three
end select
exitFunction:
exit funtion
one:
.....
goto exitFunction
two:
............... ..
goto exitFunction
three:
............... .
goto exitFunction
end function


I operate under the theory that an ugly system that works is better than
a pretty system that doesn't. If it works for you and you are happy as
a clam, then go ahead.

That being said, if I were to hire someone that coded that way I would
give him/her one warning and the second time I saw him/her code like
that I would fire that person.

Also, if I were to come in behind the person that got fired I would most
likely update the code unless it was an irrelevent part of the system or
would take too much time to correct in regards to the benefits.

But there is nothing wrong with it...if you are talking about the
process itself working.

I think it shows a lack of pride. It shows you don't care. It shows
you are a maverick, not a team player. It shows you like spaghetti, the
messier the better, lots of tomato stains on your shirt, pants, and the
floor. Other programmers that study your code will wonder how you got
the job. Most likely your coworker programmers will laugh at you behind
your back and your coding style will remind them of the movie The Good,
The Bad, The Ugly.

But it works. If your application is of no consequence, use it.

Just my $.02

Nov 13 '05 #2
Dear Caesar Salad

You made me laugh. But you never really told me what is wrong with it.

In my example, it seems to me that I make the code easier, not harder to
understand, by breaking it up into parts. You read the main code to get the
gist of things and if you are interested then you can check on the
individual sub-routines. Some of these sub-routines may be lengthy and
would get in the way?

I mean, how would you write a function like the one I put below???

I definitely am a maverick, but that is not necessarily a bad thing (I
hope). Here in NZ we like to do things our own way.

Thanks for your reply

Nicolaas


"Salad" <oi*@vinegar.co m> wrote in message
news:j5******** ***********@new sread1.news.pas .earthlink.net. ..
WindAndWaves wrote:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE.

I can understand that it may lead to confusing code, but I often use it like this:

is this wrong?????

Function x
select case z
case 1
goto one
case 2
goto two
case else
goto three
end select
exitFunction:
exit funtion
one:
.....
goto exitFunction
two:
............... ..
goto exitFunction
three:
............... .
goto exitFunction
end function


I operate under the theory that an ugly system that works is better than
a pretty system that doesn't. If it works for you and you are happy as
a clam, then go ahead.

That being said, if I were to hire someone that coded that way I would
give him/her one warning and the second time I saw him/her code like
that I would fire that person.

Also, if I were to come in behind the person that got fired I would most
likely update the code unless it was an irrelevent part of the system or
would take too much time to correct in regards to the benefits.

But there is nothing wrong with it...if you are talking about the
process itself working.

I think it shows a lack of pride. It shows you don't care. It shows
you are a maverick, not a team player. It shows you like spaghetti, the
messier the better, lots of tomato stains on your shirt, pants, and the
floor. Other programmers that study your code will wonder how you got
the job. Most likely your coworker programmers will laugh at you behind
your back and your coding style will remind them of the movie The Good,
The Bad, The Ugly.

But it works. If your application is of no consequence, use it.

Just my $.02

---
Please immediately let us know (by phone or return email) if (a) this email
contains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.698 / Virus Database: 455 - Release Date: 02/06/2004
Nov 13 '05 #3
Hi Nicolaas

The way you've written your code is like a bit of spaghetti, all over the
place within the same sub/function routine. If your one: two: & three:
labels contain complex code or many lines it would pay to create separate
subs/functions and call those subs/functions from the case one etc. This way
they can be tested and modified completely separately from the main
sub/function and the flow of the executed code follows a logical path, top
to bottom, not top to bottom then exit out the middle. My example below:

Function x
Select Case z
Case one
Call Subby1(z)
Case two
t = Fun2(z)
Case 3
t = Fun3(z)
End Select
x = t
ExitFun:
Exit Function
ErrTrap:
MsgBox "?????"
Resume ExitFun
End Function

Sub Subby1(e)
.....
.....
End Sub

Function Fun2(g) As Whatever
i = g * 3
Fun2 = i
End Function

Function Fun3(p) As Whatever
y = p * 6
Fun3 = y
End Function

If you're only going to have a few lines of code to execute for each Case
condition, place them under the Case conditions.
Try to follow the flow of this example using your logic (some obvious lines
omitted):
Sub Temp
Do Until rst.EOF
.MoveFirst
Select Case rst!City
Case "Auckland"
goto one
Case "Christchur ch"
goto two
Case "Wellington "
goto three
End Select
.MoveNext
Loop
TempExit:
Exit Sub
One:
MsgBox "City of Sails"
Two:
MsgBox "Smog City"
Three:
MsgBox "Windy City"
End Sub

I haven't tested the above code but will try it tonight.

Stewart
"WindAndWav es" <ac****@ngaru.c om> wrote in message
news:bt******** **********@news .xtra.co.nz...
Dear Caesar Salad

You made me laugh. But you never really told me what is wrong with it.

In my example, it seems to me that I make the code easier, not harder to
understand, by breaking it up into parts. You read the main code to get the gist of things and if you are interested then you can check on the
individual sub-routines. Some of these sub-routines may be lengthy and
would get in the way?

I mean, how would you write a function like the one I put below???

I definitely am a maverick, but that is not necessarily a bad thing (I
hope). Here in NZ we like to do things our own way.

Thanks for your reply

Nicolaas


"Salad" <oi*@vinegar.co m> wrote in message
news:j5******** ***********@new sread1.news.pas .earthlink.net. ..
WindAndWaves wrote:
Can anyone tell me what is wrong with the goto command. I noticed it
is
one of those NEVER USE.

I can understand that it may lead to confusing code, but I often use
it
like this:

is this wrong?????

Function x
select case z
case 1
goto one
case 2
goto two
case else
goto three
end select
exitFunction:
exit funtion
one:
.....
goto exitFunction
two:
............... ..
goto exitFunction
three:
............... .
goto exitFunction
end function

I operate under the theory that an ugly system that works is better than
a pretty system that doesn't. If it works for you and you are happy as
a clam, then go ahead.

That being said, if I were to hire someone that coded that way I would
give him/her one warning and the second time I saw him/her code like
that I would fire that person.

Also, if I were to come in behind the person that got fired I would most
likely update the code unless it was an irrelevent part of the system or
would take too much time to correct in regards to the benefits.

But there is nothing wrong with it...if you are talking about the
process itself working.

I think it shows a lack of pride. It shows you don't care. It shows
you are a maverick, not a team player. It shows you like spaghetti, the
messier the better, lots of tomato stains on your shirt, pants, and the
floor. Other programmers that study your code will wonder how you got
the job. Most likely your coworker programmers will laugh at you behind
your back and your coding style will remind them of the movie The Good,
The Bad, The Ugly.

But it works. If your application is of no consequence, use it.

Just my $.02

---
Please immediately let us know (by phone or return email) if (a) this

email contains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.698 / Virus Database: 455 - Release Date: 02/06/2004

Nov 13 '05 #4
Thank you stewart. I really appreciate your comments.
---
Please immediately let us know (by phone or return email) if (a) this email
contains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.698 / Virus Database: 455 - Release Date: 02/06/2004
Nov 13 '05 #5
I've tried out the last bit of code I sent you; when using the goto label
from within a loop you can't get back into the loop.

Stewart

"WindAndWav es" <ac****@ngaru.c om> wrote in message
news:8H******** **********@news .xtra.co.nz...
Thank you stewart. I really appreciate your comments.
---
Please immediately let us know (by phone or return email) if (a) this email contains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.698 / Virus Database: 455 - Release Date: 02/06/2004

Nov 13 '05 #6
what do you mean???
from within a loop you can't get back into the loop.


---
Please immediately let us know (by phone or return email) if (a) this email
contains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.698 / Virus Database: 455 - Release Date: 02/06/2004
Nov 13 '05 #7
WindAndWaves wrote:
Dear Caesar Salad

You made me laugh. But you never really told me what is wrong with it.
I'm glad you laughed.

There is nothing wrong with with the code. It works. In programming,
that is the bottom line...creating something that works.

However, your code examples is called spaghetti code. Spaghetti is a
bunch of noodles swimming around on a plate usually with some topping.
The start and end of the noodles may not be easy to see.

In days past, people used GoTo a lot. Yours is a very simple example.
However, lets say that you wanted to create more GoTos in the GoTos.
All of a sudden you've added complexity to the subroutine. And if you
add GoTos in those GoTos all of a sudden you have a mess. Then you if
you have a problem you are jumping all over the routine trying to figure
out what's what.

That is why there are functions and procedures. You can break a task
into segments and return values or performs some process. It adds
readability to the program. It may help in debugging.

Your code example is small. Remember that. But it would be very easy
to expand it so that it would be difficult to follow. In a routine you
want to see the start, the process, and the end in easy or as-easy to
follow increments as you can.

You don't want to come back to this code later on and cry "Holy Smoke,
what the heck is that?"

In my example, it seems to me that I make the code easier, not harder to
understand, by breaking it up into parts. You read the main code to get the
gist of things and if you are interested then you can check on the
individual sub-routines. Some of these sub-routines may be lengthy and
would get in the way?

I mean, how would you write a function like the one I put below???
Stewart gave a good example.

I definitely am a maverick, but that is not necessarily a bad thing (I
hope). Here in NZ we like to do things our own way.


I think most good programmers are mavericks. The thing is that you
don't want to get so far away from the herd (newsgroup help responders)
you're alone in the hot desert and the water hole is a mirage (newsgroup
responders provide no help or answers).

If this is a personal program that only you will ever work on, then do
what you want. Nobody will laugh. Nobody will cry "Why ME!" Any
arbitrary rules can be broken. The goal is to create something that
works. If you plan on passing on this app to a subordinate in the
future due to a promotion or perhaps take another job or even go on to
another project before you retire, it is worthwhile to develop good
habits now.
Nov 13 '05 #8
Create a table called tblCities and add 6 cities where three of them must be
Auckland, Christchurch and Wellington and paste the following code into a
standard module:

Sub Temp()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordse t("tblCities" )
With rst
.MoveFirst
Do Until .EOF
Select Case !City
Case "Auckland"
GoTo One
Case "Christchur ch"
GoTo Two
Case "Wellington "
GoTo Three
Case Else
GoTo Four
End Select
.MoveNext
Loop
End With
One:
MsgBox "City of Sails"
Two:
MsgBox "Smog City"
Three:
MsgBox "Windy City"
Four:
MsgBox "Unknown nickname"
Temp_Exit:
On Error Resume Next
rst.Close
Set rst = Nothing
Set db = Nothing
Exit Sub
Temp_Err:
MsgBox Err.Number & " " & Err.Description , vbInformation, "Temp Error"
Resume Temp_Exit
End Sub

Place a break point on the line starting With rst by placing your cursor on
that line and press <F9>. Open up the immediate window (Ctrl + G) and run
the routine by typing Temp and pressing <Enter>. Press <F8> to follow the
code line by line and you'll see that when you exit the loop using the goto
command you won't get back into it again. If you have 6 records (Cities) you
should go through the Do Until Loop 6 times and have 6 MsgBox's appear.

Stewart
"WindAndWav es" <ac****@ngaru.c om> wrote in message
news:UY******** **********@news .xtra.co.nz...
what do you mean???
from within a loop you can't get back into the loop.
---
Please immediately let us know (by phone or return email) if (a) this

email contains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.698 / Virus Database: 455 - Release Date: 02/06/2004

Nov 13 '05 #9
On Mon, 7 Jun 2004 14:03:41 +1200, "WindAndWav es" <ac****@ngaru.c om> wrote:
Can anyone tell me what is wrong with the goto command. I noticed it is one
of those NEVER USE.


All of the previous replies are great, but I also wanted to add that the truth
is Gotos are things you should mostly avoid, but they can be used well in very
specific circumstances, and with proper consideration. Use them only when
there's a good reason to do so, and where they don't add complexity or
increase the likelihood of unwanted side-effects creeping into the code.

An example of a good way to use Gotos is to skip the majority of a procedure
when it would be irrelevant. A Goto, when used in this way, is known as a
Guard Clause. The alternative is to nest the majority of the procedure in an
If block, and if there are several of them, the body of the code may be nested
several levels deep.

Without guard clauses...
Sub Foo(colBar As VBA.Collection)
If Not (objBar Is Nothing) Then
If objBar.Count > 0 Then
If objBar(1) <> "-" Then
' Code body
' ...
' ...
' ...
End If
End If
End If
' Clean-up code.
' ...
End Sub

With guard clauses...
Sub Foo(colBar As VBA.Collection)
If objBar Is Nothing Then Goto Clean_Up_And_Ex it
If objBar.Count = 0 Then Goto Clean_Up_And_Ex it
If objBar(1) = "-" Then Goto Clean_Up_And_Ex it
' Code body
' ...
' ...
' ...
Clean_Up_And_Ex it:
' Clean-up code.
' ...
End Sub

Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
26644
by: electrician | last post by:
Perl has it, Basic has it, Fortran has it. What is so difficult about creating a goto command for JavaScript. Just set up a label and say go to it.
5
3732
by: Eng Teng | last post by:
What is the command to display computer name & ip address in VB.NET 2005 (OS WindowsXP & Windows Vista) Regards, Tee
1
1249
by: a | last post by:
Dear Frends I have this code but I don't know the SQL Code to copy data from CSV file to new table in the current access data base What is SQl Command needed??? Imports System.Data.OleDb Dim cn As New OleDbConnection cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
3
7344
by: muddasirmunir | last post by:
I am trying to give the following Icon in my form in vb6. http://www.mediafire.com/?ymzgkgyi50j But , when I put this in my form I got error "Invalid Picutre" What wrong in it? How to add this in my forms?
0
8774
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.