473,786 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

when GOTO makes sense.

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

Nov 13 '05
45 2729


On 12/7/2003 4:00 PM, Barbrawl McBribe wrote:
Debashish Chakravarty <de********@yah oo.com> wrote in message news:<3F******* *******@yahoo.c om>...
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.

Breaking out of complex loops?? That's the O'Reilly example at any rate.


So, imagine you have to maintain some code. You're new to the domain, trying to
make sense of 1000 lines of code in a hurry to fix a problem that a customer is
threatening to apply penalty clauses for. You're knee deep in, say, a complex
nested loop and suddenly, after a test on a couple of variables and a function
return code, you find a line that says "goto cleanup;". Can you say
"Aaaarrgggghhh! !!!"? Wouldn't a nice little variable that describes the
condition under which you're exiting the loop be a more pleasant discovery?

Ed.

Nov 13 '05 #41
Dan Pop wrote:
CBFalconer <cb********@yah oo.com> writes:
.... snip ...
However I have found no /reasonable/ way to regression test my
nmalloc package, since the pointers it returns depend on the
state of the rest of the machine.


If you have problems testing a malloc package, it's most likely
due to its extremely bad design.


I think it is high time for you to take Richards refresher course
in Reading for Comprehension, after which you can consider the
"/reasonable/" and "regression test" above, and the implication
(from the snipped portion) of "automated" .

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #42
In <3F************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
Dan Pop wrote:
CBFalconer <cb********@yah oo.com> writes:

... snip ...
> However I have found no /reasonable/ way to regression test my
> nmalloc package, since the pointers it returns depend on the
> state of the rest of the machine.


If you have problems testing a malloc package, it's most likely
due to its extremely bad design.


I think it is high time for you to take Richards refresher course
in Reading for Comprehension, after which you can consider the
"/reasonable/" and "regression test" above, and the implication
(from the snipped portion) of "automated" .


If you have a valid point to make, it's much better to simply make it.
No amount of sarcasm can replace it.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #43
Damn Skippy <sk****@damn.in valid.org> wrote in message news:<tp******* *************** **********@4ax. com>...
On Mon, 01 Dec 2003 11:25:41 -0600, Ed Morton
<mo************ ****@lucent.com > wrote:
I'm not 100% against gotos, I've just never seen code that uses gotos
that I personally find more readable than the equivalent without gotos.
I recently wrote a short (200 lines or so) program using gotos just to
see if I could learn to appreciate the benefits wrt error-handling, but
once the error-handling became even moderatly complex (i.e. realisitc!)
the initial simplicity afforded by the gotos fell away. I looked at your
code hoping to learn a good way to use goto for readability.

Ed.
To me,

if ( a) { ... }

reads the same as

if (!a) goto skip_this; { ... } skip_this:

especially, and i will be flamed for this, if you consider the opcodes
which will be generated, on the actual real tangible machine(s) on
which it will run. When you think of it this way, it doesn't matter
whether you use goto or not, cos the CPU will (i.e. JUMP type of
instruction).


Most compilers are optimizing anyway, or at least become so with the
addition of options. gcc, for example, probably knows more about
opcode scheduling on Intel chips than I'll ever want or need to (how
to avoid cache misses, pairing rules, etc.), so I can trust it to
generate optimal code even if I give it human-readable C.

I think the impact of optimizing compilers plus the continuous
increases in chip speed negate the whole notion of manually optimizing
C in that way. If you need to speed things up, give the compiler a few
options. If you really need a speedup, choose a different algorithm.
As a last resort, hand-code some assembly.

So you must decide what is readable, and what other people will think
is readable. Bottom line - if people keep asking you why you have all
those 'goto' statements, it's time to cut back.
Of course, some people will flame you for having any goto statements.
Or break statements. Or subroutines with more than one return
statement. Or assignments. Or ... ;)

Remember, C code will be:
1) compiled and executed or
2) interpreted or
3) in a textbook, hopefully with rules and exceptions
That's probably true, but you still can trust the compiler farther
than you seem willing to.


============ inefficient, inelegant, recommended ===========
Inefficient is relative: If a Pentium burns a few thousand clock
cycles on inefficient assembly, what have I actually lost? Less than a
second.

Inelegant is also in the eye of the beholder: I think the code below
is perfectly readable.

for ( .. ) {
if ( a != b ) {
good=false;
complain();
break;
}
}

if (good) {
process();
}

cleanup_regardl ess:
This label isn't referenced by anything yet. I don't know if that's
warnable, but it certainly looks odd.
cleanup();

============ better? saves one test and one stack variable ===========
Maybe on some machines with some compiler options.

And it isn't very structured: What happens when you want to change the
behavior of the code, perhaps add a test for some edge case in some
instances, instead of simply jumping straght to cleanup_regardl ess
every time? With a subroutine, you simply add a value you can pass in.
With a goto, you either duplicate code to check for the edge case
everywhere that needs it right before the goto (a very poor solution),
you use a global you check at the point you jump to (another poor
solution), or you add another label in your code and modify goto
statements (a third poor substitute for good planning).

for ( .. ) {
if ( a != b ) {
complain();
goto cleanup_regardl ess;
}
}

process();

cleanup_regardl ess:
cleanup();

============ ===========

of course, i hope i never see gotos in something like:
I don't think anyone is here to condone that. I think there would be
more people willing to defend the ALTER verb in COBOL than that mess.

if ( (fp = fopen(szFilenam e)) != NULL ) {
if ( fread (&buf, 1, len, fp) == len ) {
if ( !strncmp ( buf, 4, "JFIF") ) {
process_jfif();
}
}
}

Nov 14 '05 #44
In article <br**********@s unnews.cern.ch> , Da*****@cern.ch says...
In <3F************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
I think it is high time for you to take Richards refresher course
in Reading for Comprehension, after which you can consider the
"/reasonable/" and "regression test" above, and the implication
(from the snipped portion) of "automated" .


If you have a valid point to make, it's much better to simply make it.
No amount of sarcasm can replace it.

Dan


No question, the use of terms and phrases such as "stupid", "idiot",
"engage your brain", "what a stupid poster", "are you so pathetically
stupid", "If you're so stupid that ...", "This is downright idiotic",
"moron", etc. are far better ways to make a point, right Dan?

--
Randy Howard _o
2reply remove FOOBAR \<,
_______________ _______()/ ()_____________ _______________ _______________ ___
SCO Spam-magnet: po********@sco. com
Nov 14 '05 #45
In <MP************ ************@ne ws.megapathdsl. net> Randy Howard <ra**********@F OOmegapathdslBA R.net> writes:
In article <br**********@s unnews.cern.ch> , Da*****@cern.ch says...
In <3F************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
>I think it is high time for you to take Richards refresher course
>in Reading for Comprehension, after which you can consider the
>"/reasonable/" and "regression test" above, and the implication
>(from the snipped portion) of "automated" .


If you have a valid point to make, it's much better to simply make it.
No amount of sarcasm can replace it.

Dan


No question, the use of terms and phrases such as "stupid", "idiot",
"engage your brain", "what a stupid poster", "are you so pathetically
stupid", "If you're so stupid that ...", "This is downright idiotic",
"moron", etc. are far better ways to make a point, right Dan?


I never use them *instead* of making a point, but in addition to it.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #46

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

Similar topics

25
4652
by: BOOGIEMAN | last post by:
I've just finished reading Python turtorial for non-programmers and I haven't found there anything about some usefull commands I used in QBasic. First of all, what's Python command equivalent to QBasic's "goto" ? Secondly, how do I clear screen (cls) from text and other content ? And last, how do I put program to wait certain amount of seconds ? If I remeber correctly I used to type "Wait 10" and QBasic waits 10 seconds before proceeding...
36
6740
by: Michael | last post by:
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
5
1795
by: deko | last post by:
I've heard it said that you only want to use a number (e.g. integer, long, etc.) if you are going to do calculations or some kind of math with it. Is this true? For example, I run a validate routine that checks an address entry - if something's missing in the entry, the code does different things based on what is missing, indicated my a 2 or a 3 - integer values. Should I use string data types here? Does it matter? Public Sub...
51
13388
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
17
2319
by: Mike Hofer | last post by:
While I'd toyed with C, C++, and Java over the last 20 years or so, my principal language has been BASIC, QBASIC, then Visual Basic, and finally Visual Basic .NET. But lately, I've been using C# and I absolutely *love* it. It makes me think more about what I'm doing it before I just spew code into the editor. I'm writing better code than ever. The only thing so far that I don't like about it is the switch construct. I can't do this:
77
4040
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.
8
2259
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an exception should be thrown only in exceptional situations. It turned out that each of my colleagues had their own interpretation about what an "exceptional situation" may actually be. First of all, myself I’m against using exceptions extensively,...
34
26648
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.
59
5066
by: raashid bhatt | last post by:
why are GOTO's not used they just a simple JMP instructions what's bad about them
0
9647
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
9491
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
10357
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
10104
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
6744
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
5397
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...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
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
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.