473,807 Members | 2,766 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

goto

Hi,
I know I know its notoriously bad! I don't want to use it. I was wondering,
does it still exist? If it does, I really don't understand how!? like what
happens if you just goto halfway through a function (no objects properly
constructed!) , or a constructor itself??
Just intrigued!!

Mike
Jul 23 '05
36 6742
On Sat, 26 Mar 2005 12:35:13 GMT, "Phlip" <ph*******@yaho o.com> wrote:
>I know I know its notoriously bad! I don't want to use it. Why not? Goto is very useful since there is not labelled break. When
you have nested for in another for /for example/ the easiest way /and
elegant too/ to break from the external one is to use goto.


or return.


AFAIK return breaks the entire function ;-).
Why is your function so long?


I don't think that function 10-lines long is "so long". And I am
talking about pretty obvious

for (int x=0;x<width;++x )
for (int y=0;y<height;++ y)
if (something())
break_the_x_loo p;

and there is nice goto construction.

I just pointed the use of goto, if you are not convinced -- ok, it is
not Java, write your programs as you like.

have a nice day
bye

PS. On the other hand, such situations are rather rare -- in project
~40,000 lines long I had to use goto once :-)
--
Maciej "MACiAS" Pilichowski http://bantu.fm.interia.pl/

M A R G O T --> http://www.margot.cad.pl/
automatyczny t³umacz (wczesna wersja rozwojowa) angielsko-polski
Jul 23 '05 #11
Maciej Pilichowski wrote:
I don't think that function 10-lines long is "so long". And I am
talking about pretty obvious

for (int x=0;x<width;++x ) { for (int y=0;y<height;++ y) if (something())
break;

if(something())
break;
}

and there is nice goto construction.

Which can be avoided.

--
Ioannis Vranos

http://www23.brinkster.com/noicys

[I have set the word-wrapping at 90 characters. I am interested in hearing if it causes
inconvenience to anyone].
Jul 23 '05 #12
On Sun, 27 Mar 2005 09:18:00 +0200, Ioannis Vranos
<iv*@remove.thi s.grad.com> wrote:
Maciej Pilichowski wrote:
I don't think that function 10-lines long is "so long". And I am
talking about pretty obvious

for (int x=0;x<width;++x )

{
for (int y=0;y<height;++ y)

if (something())
break;

if(something())
break;
}


You called something() twice, what if it has side-effects (like I/O)?

Yes, you can get round that with a boolean flag:

bool running = true;
for (int x = 0; running && x < width; ++x)
for (int y = 0; y < height; ++y)
if (something())
running = false;

but that is less obvious and in the case of more complicated loops can
obscure the actual flow.
and there is nice goto construction.


Which can be avoided.


Of course it can, so can most things. There are however occasions when
goto (and return in the middle of a function, which many ivory tower
computer scientists regard as nearly as bad) are the cleanest and most
maintainable constructions.

Just like the misquotation "Money is the root of all evil", saying that
all uses of goto are evil misses the point. Certainly /some/ uses of
goto are dangerousm just like /some/ uses of money, but the thing itself
is a tool nothing more.

As the C++ FAQ Lite says:

[6.14] What does the FAQ mean by "such and such is evil"?

It means such and such is something you should avoid most of the
time, but not something you should avoid all the time. For example,
you will end up using these "evil" things whenever they are "the
least evil of the evil alternatives." It's a joke, okay? Don't take
it too seriously.

...

Another thing: things labeled as "evil" (macros, arrays, pointers,
etc.) aren't always bad in all situations. When they are the "least
bad" of the alternatives, use them!

And as we know from the FDA, anything in excess can be "considered
harmful". Sugar, water, oxygen; goto, pointers, macros... People who
try to write everything as OO whether the problem domain matches it or
not. People who think "self-documenting code" means that all
identifiers have to be complete sentences. People insisting on "one
true way" in any field...

Fanaticism is the true evil. Death to all fanatics! The must be
moderation in all things (especially in moderation).

Chris C
Jul 23 '05 #13
Chris Croughton wrote:
You called something() twice, what if it has side-effects (like I/O)?

Yes, you can get round that with a boolean flag:

bool running = true;
for (int x = 0; running && x < width; ++x)
for (int y = 0; y < height; ++y)
if (something())
running = false;

but that is less obvious and in the case of more complicated loops can
obscure the actual flow.

or in this case:
return_type res;

for(int x=0; x<width; ++x)
{
for(int y=0; y<height; ++y)
{
res= something();

if(return_type)
break;
}
if(return_type)
break;
}
Of course it can, so can most things. There are however occasions when
goto (and return in the middle of a function, which many ivory tower
computer scientists regard as nearly as bad) are the cleanest and most
maintainable constructions.

Just like the misquotation "Money is the root of all evil", saying that
all uses of goto are evil misses the point. Certainly /some/ uses of
goto are dangerousm just like /some/ uses of money, but the thing itself
is a tool nothing more.

As the C++ FAQ Lite says:

[6.14] What does the FAQ mean by "such and such is evil"?

It means such and such is something you should avoid most of the
time, but not something you should avoid all the time. For example,
you will end up using these "evil" things whenever they are "the
least evil of the evil alternatives." It's a joke, okay? Don't take
it too seriously.

...

Another thing: things labeled as "evil" (macros, arrays, pointers,
etc.) aren't always bad in all situations. When they are the "least
bad" of the alternatives, use them!

However I haven't used goto since some time back in school, when I was checking
GWBASIC/QBasic then. So, this means it is not much needed, for regular application
programming at least.

I can understand its use on machine-produced C++ code and in a very desperate need for
performance, when all other alternatives are exhausted.

--
Ioannis Vranos

http://www23.brinkster.com/noicys

[I am using 90 characters word-wrapping - (800/640) *72= 90 or better described as:
(800/640) *80 - 10 for quotation= 90. If someone finds it inconvenient, please let me know].
Jul 23 '05 #14
On Sun, 27 Mar 2005 17:30:34 +0300, Ioannis Vranos
<iv*@remove.thi s.grad.com> wrote:
Chris Croughton wrote:
You called something() twice, what if it has side-effects (like I/O)?

Yes, you can get round that with a boolean flag:

bool running = true;
for (int x = 0; running && x < width; ++x)
for (int y = 0; y < height; ++y)
if (something())
running = false;
An extra break; is needed, or a test in te inner loop for running, I
chopped it down too far:

bool running = true;
for (int x = 0; running && x < width; ++x)
for (int y = 0; running && y < height; ++y)
if (something())
running = false;
but that is less obvious and in the case of more complicated loops can
obscure the actual flow.
or in this case:

return_type res;

for(int x=0; x<width; ++x)
{
for(int y=0; y<height; ++y)
{
res= something();

if(return_type)


You meant res, I assume <g>. And assuming that return_type evaluates to
non-zero if the exit is needed.
break;
}

if(return_type)
break;
}
Of course it can, so can most things. There are however occasions when
goto (and return in the middle of a function, which many ivory tower
computer scientists regard as nearly as bad) are the cleanest and most
maintainable constructions.
However I haven't used goto since some time back in school, when I was checking
GWBASIC/QBasic then. So, this means it is not much needed, for regular application
programming at least.


I don't use it either, but I do return from funtions in the middle, or
particularly near the start if conditions aren't met, which many people
regard as just as bad:

int func(int param)
{
if (param < 0)
return FAIL_VAL_1;
/* do stuff setting up conditions */
if (error_setting_ conditions)
return FAIL_VAL_2;
/* do the actual stuff */
return SUCCESS_VAL;
}

However, I've seen goto used with good and clear effect as well.
I can understand its use on machine-produced C++ code and in a very desperate need for
performance, when all other alternatives are exhausted.


That's not the only time it's useful.

Chris C
Jul 23 '05 #15
In article <1111907881.612 35@athnrd02>,
Ioannis Vranos <iv*@remove.thi s.grad.com> wrote:
Which can be avoided.


But is sometimes the best tool for the job. Ignoring your best tool
because of dogma is idiotic.
--
Mark Ping
em****@soda.CSU A.Berkeley.EDU
Jul 23 '05 #16
E. Mark Ping wrote:
In article <1111907881.612 35@athnrd02>,
Ioannis Vranos <iv*@remove.thi s.grad.com> wrote:
Which can be avoided.

But is sometimes the best tool for the job. Ignoring your best tool
because of dogma is idiotic.


most of the time though, its an indicator that the design smells.

In the case of the example code within this thread, the nested for loops
are searching for something within an X & Y co-ordinate within a certain
width & height dimension.

When the target was originally placed at the co-ordinate, we could have
had the Dimension object keep a mapping co-ordinate -> target or target
-> co-ordinate, etc...

Then we could have queried and retrieved the target from the Dimension
Also, Goto's make refactoring very difficult.
Jul 23 '05 #17
Chris Croughton wrote:
On Sun, 27 Mar 2005 17:30:34 +0300, Ioannis Vranos
<iv*@remove.thi s.grad.com> wrote:

Chris Croughton wrote:

snipped

I don't use it either, but I do return from funtions in the middle, or
particularly near the start if conditions aren't met, which many people
regard as just as bad:

int func(int param)
{
if (param < 0)
return FAIL_VAL_1;
/* do stuff setting up conditions */
if (error_setting_ conditions)
return FAIL_VAL_2;
/* do the actual stuff */
return SUCCESS_VAL;
}

There is nothing wrong with a function of that length having multiple
returns. Its very clear.

The problem with multiple returns within a single function is when the
functions are long. The longer a function to harder it is to see the
return points. By long I mean more than a hand full of lines, my
personal line per function limit is around 7. Comes from the human
minds ability to deal with groupings of 7 or less easily. (Its one
reason why your credit card numbers are bunched into groups of 4 digits,
and why telephone numbers typically have an area code and actual number
as two groupings).

Guard clauses as your example above are a great technique for aiding the
reader.

The worst Guard clauses are when the logic is inverted resulting in the
entire function body being indented excessively.

e.g. your code with badly implemented clauses.

int func(int param)
{
if (param > 0)
{
/* do stuff setting up conditions */
if (! error_setting_c onditions )
{
/* do the actual stuff */
return SUCCESS_VAL;

} else
return FAIL_VAL_2;

} else
return FAIL_VAL_1;
}
I see this type of code all to often unfortunately.
Jul 23 '05 #18
Chris Croughton wrote:
return_type res;

for(int x=0; x<width; ++x)
{
for(int y=0; y<height; ++y)
{
res= something();

if(return_type)

You meant res, I assume <g>. And assuming that return_type evaluates to
non-zero if the exit is needed.

Yes.


break;
}

if(return_type)
break;
}
However I haven't used goto since some time back in school, when I was checking
GWBASIC/QBasic then. So, this means it is not much needed, for regular application
programming at least.

I don't use it either, but I do return from funtions in the middle, or
particularly near the start if conditions aren't met, which many people
regard as just as bad:

int func(int param)
{
if (param < 0)
return FAIL_VAL_1;
/* do stuff setting up conditions */
if (error_setting_ conditions)
return FAIL_VAL_2;
/* do the actual stuff */
return SUCCESS_VAL;
}


I know some say that this is bad, I but do not agree with them. This is an unneeded
constraint, just to make our lives more difficult (or in other words, they think that by
imposing such restrictive rules will be able to protect bad programmers from bad programming).
However, I've seen goto used with good and clear effect as well.

I can understand the use of goto for the situations that I described. For example, in
application programming, in non-desperate for speed conditions, why should one use goto?

I can understand its use on machine-produced C++ code and in a very desperate need for
performance , when all other alternatives are exhausted.

That's not the only time it's useful.

May you say in what other conditions it is useful?

--
Ioannis Vranos

http://www23.brinkster.com/noicys

[I am using 90 characters word-wrapping - (800/640) *72= 90 or better described as:
(800/640) *80 - 10 for quotation= 90. If someone finds it inconvenient, please let me know].
Jul 23 '05 #19
In article <d2**********@n ews.freedom2sur f.net>,
Andrew McDonagh <ne**@us.com> wrote:
most of the time though, its an indicator that the design smells.


In 10+ years of C/C++ coding I've never seen goto used except to exit
from an inner loop, which is where it's commonly recommended.
--
Mark Ping
em****@soda.CSU A.Berkeley.EDU
Jul 23 '05 #20

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

Similar topics

30
4281
by: Hayri ERDENER | last post by:
hi, what is the equivalent of C languages' goto statement in python? best regards
51
13393
by: WindAndWaves | last post by:
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
37
3262
by: Tim Marshall | last post by:
From http://www.mvps.org/access/tencommandments.htm 9th item: Thou shalt not use "SendKeys", "Smart Codes" or "GoTo" (unless the GoTo be part of an OnError process) for these will lead you from the path of righteousness. What about also using it as a means of exiting a procedure?
52
21986
by: Rick | last post by:
Hi, For portability, can an issue arise if we use while(1) for an infinite loop in C? If so, should we then use for(;;)? Thanks, Rick
45
2735
by: Debashish Chakravarty | last post by:
K&R pg.66 describes two situations when using goto makes sense. Has anyone here come across situations where using goto provided the most elegant solution. -- http://www.kashmiri-pandit.org/atrocities/index.html
77
4043
by: M.B | last post by:
Guys, Need some of your opinion on an oft beaten track We have an option of using "goto" in C language, but most testbooks (even K&R) advice against use of it. My personal experience was that goto sometimes makes program some more cleaner and easy to understand and also quite useful (in error handling cases). So why goto is outlawed from civilized c programmers community. is there any technical inefficiency in that.
34
26651
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.
3
2195
by: electrician | last post by:
Yes, no GOTO. This is a major blunder on part of the creators of these tools. GOTO gives the programmer the absolute control over the program. Yes, no matter what, a GOTO sends the program to where YOU want and takes the power away from the mean spirited CREATORS. Yes, even GOD gave us free will! But the creators of JavaScript have decided they will forever interfere in how YOU program. THEY have decided to keep GOTO to themselves...
17
2621
by: SoftEast | last post by:
Hi Buddies, I have read a lot of stuffs regarding not using GOTO statements to opt a good programming style http://david.tribble.com/text/goto.html]. Can anybody give a particular lines of code which shows harmfullness of GOTO. SoftEast...
59
5080
by: raashid bhatt | last post by:
why are GOTO's not used they just a simple JMP instructions what's bad about them
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9599
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
10624
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...
0
10111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
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
6877
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
5546
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...
2
3853
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
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.