Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #55792 > unrolled thread
| Started by | "S.K.R. de Jong" <SKRdJ@nowhere.net> |
|---|---|
| First post | 2015-01-04 00:46 +0000 |
| Last post | 2015-01-08 12:03 -0500 |
| Articles | 20 on this page of 102 — 30 participants |
Back to article view | Back to comp.lang.c
Is this poor style? "S.K.R. de Jong" <SKRdJ@nowhere.net> - 2015-01-04 00:46 +0000
Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-03 16:58 -0800
Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-04 20:37 +1300
Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-04 02:32 -0800
Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-05 09:39 +1300
Re: Is this poor style? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-01-04 22:53 +0000
Re: Is this poor style? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-04 01:29 +0000
Re: Is this poor style? Morris Dovey <mrdovey@iedu.com> - 2015-01-04 00:00 -0600
Re: Is this poor style? Morris Dovey <mrdovey@iedu.com> - 2015-01-04 02:20 -0600
Re: Is this poor style? August Karlstrom <fusionfile@gmail.com> - 2015-01-04 15:46 +0100
Re: Is this poor style? Ike Naar <ike@iceland.freeshell.org> - 2015-01-04 15:10 +0000
Re: Is this poor style? August Karlstrom <fusionfile@gmail.com> - 2015-01-04 17:06 +0100
Re: Is this poor style? Clark Smith <noaddress@nowhere.net> - 2015-01-04 15:30 +0000
Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-04 07:44 -0800
Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-05 00:35 +0000
Re: Is this poor style? BartC <bc@freeuk.com> - 2015-01-05 01:02 +0000
Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-05 01:43 +0000
Re: Is this poor style? BartC <bc@freeuk.com> - 2015-01-05 11:35 +0000
Re: Is this poor style? raltbos@xs4all.nl (Richard Bos) - 2015-01-05 12:58 +0000
Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-05 13:14 +0000
Re: Is this poor style? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-01-05 22:02 +0000
Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-05 14:24 -0800
Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-06 12:26 +1300
Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-06 02:40 -0800
Re: Is this poor style? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-01-07 19:32 +0000
Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-07 20:03 +0000
Re: Is this poor style? BartC <bc@freeuk.com> - 2015-01-07 20:27 +0000
Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-08 09:55 +1300
Re: Is this poor style? Les Cargill <lcargill99@comcast.com> - 2015-01-07 18:13 -0600
Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-08 13:17 +1300
Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-07 21:02 +0000
Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-08 11:22 +0100
Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-08 11:33 +0000
Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-08 13:08 +0100
Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-08 12:54 +0000
Re: Is this poor style? David Brown <david.brown@hesbynett.no> - 2015-01-08 14:04 +0100
Re: Is this poor style? Keith Thompson <kst-u@mib.org> - 2015-01-07 13:57 -0800
Re: Is this poor style? Les Cargill <lcargill99@comcast.com> - 2015-01-07 18:12 -0600
Re: Is this poor style? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-07 20:56 +0000
Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-07 21:18 +0000
Re: Is this poor style? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-07 21:28 +0000
Re: Is this poor style? BartC <bc@freeuk.com> - 2015-01-06 11:28 +0000
Re: Is this poor style? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-01-07 20:19 +0000
Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-07 16:05 -0800
Re: Is this poor style? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-08 08:29 +0000
Re: Is this poor style? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-01-12 22:08 +0000
Re: Is this poor style? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-24 20:35 -0700
Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-25 08:43 -0700
Re: Is this poor style? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 16:38 -0700
Re: Is this poor style? Stephen Sprunk <stephen@sprunk.org> - 2015-03-26 15:43 -0500
Re: Is this poor style? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-28 08:57 -0700
Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-08 13:23 +1300
Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-07 16:53 -0800
Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-08 14:11 +1300
Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-08 11:10 +1300
Re: Is this poor style? John Bode <jfbode1029@gmail.com> - 2015-01-05 15:54 -0800
Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-06 11:03 +0100
Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-06 11:10 +0100
Re: Is this poor style? John Bode <jfbode1029@gmail.com> - 2015-01-06 06:03 -0800
Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-06 14:44 +0000
Re: Is this poor style? "Osmium" <r124c4u102@comcast.net> - 2015-01-06 09:10 -0600
Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-08 11:14 +0100
Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-08 11:31 +0000
Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-08 13:06 +0100
Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-08 13:19 +0100
Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-08 12:53 +0000
Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-08 05:49 -0800
Re: Is this poor style? Keith Thompson <kst-u@mib.org> - 2015-01-08 08:18 -0800
Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-09 07:58 +0100
Re: Is this poor style? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-24 09:28 -0700
Re: Is this poor style? Keith Thompson <kst-u@mib.org> - 2015-03-24 13:57 -0700
Re: Is this poor style? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 08:28 -0700
Re: Is this poor style? Keith Thompson <kst-u@mib.org> - 2015-03-25 12:17 -0700
Re: Is this poor style? Öö Tiib <ootiib@hot.ee> - 2015-04-05 17:03 -0700
Re: Is this poor style? John Bode <jfbode1029@gmail.com> - 2015-01-14 11:25 -0800
Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-14 19:41 +0000
Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-15 09:01 +0100
Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-15 09:35 +0100
Re: Is this poor style? James Kuyper <jameskuyper@verizon.net> - 2015-01-04 11:15 -0500
Re: Is this poor style? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-01-04 21:38 +0000
Re: Is this poor style? Dr Nick <nospam-4@temporary-address.org.uk> - 2015-01-05 07:27 +0000
Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-05 06:33 -0800
Re: Is this poor style? Vincenzo Mercuri <nomail@yahoo.it> - 2015-01-04 18:56 +0100
Re: Is this poor style? rouben@shady.(none) (Rouben Rostamian) - 2015-01-04 18:22 +0000
Re: Is this poor style? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-04 21:35 +0000
Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-05 10:46 +1300
Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-04 15:36 -0800
Re: Is this poor style? asetofsymbols@gmail.com - 2015-01-05 12:05 -0800
Re: Is this poor style? asetofsymbols@gmail.com - 2015-01-05 14:18 -0800
Re: Is this poor style? asetofsymbols@gmail.com - 2015-01-05 14:32 -0800
Re: Is this poor style? asetofsymbols@gmail.com - 2015-01-05 14:37 -0800
Re: Is this poor style? James Kuyper <jameskuyper@verizon.net> - 2015-01-05 18:23 -0500
Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-05 23:32 +0000
Re: Is this poor style? Keith Thompson <kst-u@mib.org> - 2015-01-05 15:31 -0800
Re: Is this poor style? Phil Carmody <pc+usenet@asdf.org> - 2015-01-13 15:00 +0200
Re: Is this poor style? Les Cargill <lcargill99@comcast.com> - 2015-01-04 15:07 -0600
Re: Is this poor style? jadill33@gmail.com - 2015-01-05 15:03 -0800
Re: Is this poor style? buja <buja66@gmail.com> - 2015-01-06 21:48 +0100
Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-08 11:21 +0100
Re: Is this poor style? buja <buja66@gmail.com> - 2015-01-08 12:42 +0100
Re: Is this poor style? "S.K.R. de Jong" <SKRdJ@nowhere.net> - 2015-01-08 15:29 +0000
Re: Is this poor style? James Kuyper <jameskuyper@verizon.net> - 2015-01-08 12:03 -0500
Page 1 of 6 [1] 2 3 4 5 6 Next page →
| From | "S.K.R. de Jong" <SKRdJ@nowhere.net> |
|---|---|
| Date | 2015-01-04 00:46 +0000 |
| Subject | Is this poor style? |
| Message-ID | <m8a2hk$7td$1@news.albasani.net> |
I would like the opinion of people in this forum as to whether or
not the following is poor style:
int
f(char *x)
{
int diagnostic = 0;
do {
if (x == NULL)
break;
...
diagnostic = 1;
} while(0);
return diagnostic;
}
The same could be accomplished with goto, of course. Which kind of
construct is more acceptable for dealing with error conditions: the empty
while loop? goto with labels? Something else?
[toc] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-01-03 16:58 -0800 |
| Message-ID | <497d6536-dabb-4474-8a42-5f4e7a0b6054@googlegroups.com> |
| In reply to | #55792 |
On Sunday, January 4, 2015 12:46:54 AM UTC, S.K.R. de Jong wrote:
> I would like the opinion of people in this forum as to whether or
> not the following is poor style:
>
> int
> f(char *x)
> {
> int diagnostic = 0;
>
> do {
> if (x == NULL)
> break;
>
> ...
>
> diagnostic = 1;
>
> } while(0);
>
> return diagnostic;
> }
>
> The same could be accomplished with goto, of course. Which kind of
> construct is more acceptable for dealing with error conditions: the empty
> while loop? goto with labels? Something else?
Don't invent variables for the sake of avoiding a goto, and certainly
don't use a fake loop with a break just so that you can say the
word "goto" appears nowhere in your source.
I'd write like this:
int f(char *x)
{
if(x == NULL)
goto passed_null;
/* logic here */
if(x == NULL)
goto madex_null;
return 0;
passed_null:
/* clean up here */
return 1;
madex_null:
/* duplicate clean up */
return 2;
}
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2015-01-04 20:37 +1300 |
| Message-ID | <cgs8tqF55s4U1@mid.individual.net> |
| In reply to | #55793 |
Malcolm McLean wrote:
>
> Don't invent variables for the sake of avoiding a goto, and certainly
> don't use a fake loop with a break just so that you can say the
> word "goto" appears nowhere in your source.
>
> I'd write like this:
>
> int f(char *x)
> {
> if(x == NULL)
> goto passed_null;
>
> /* logic here */
> if(x == NULL)
> goto madex_null;
>
> return 0;
> passed_null:
> /* clean up here */
> return 1;
> madex_null:
> /* duplicate clean up */
> return 2;
> }
I assume you are taking the piss?
I can't think of any other reason for such gratuitous use of goto.
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-01-04 02:32 -0800 |
| Message-ID | <bad9fd8b-0490-443b-9f79-1defd64b69d8@googlegroups.com> |
| In reply to | #55799 |
On Sunday, January 4, 2015 7:37:40 AM UTC, Ian Collins wrote:
> Malcolm McLean wrote:
> >
> > Don't invent variables for the sake of avoiding a goto, and certainly
> > don't use a fake loop with a break just so that you can say the
> > word "goto" appears nowhere in your source.
> >
> > I'd write like this:
> >
> > int f(char *x)
> > {
> > if(x == NULL)
> > goto passed_null;
> >
> > /* logic here */
> > if(x == NULL)
> > goto madex_null;
> >
> > return 0;
> > passed_null:
> > /* clean up here */
> > return 1;
> > madex_null:
> > /* duplicate clean up */
> > return 2;
> > }
>
> I assume you are taking the piss?
>
> I can't think of any other reason for such gratuitous use of goto.
>
>
The perennial problem is that short code snippets don't illustrate how real code behaves.
Functions should ideally have one point of entry and one point of exit, and the error-handling
code should be distinguished from the normal flow control.
C doesn't make that easy to achieve. C++ allows you to throw an exception. In C, we have
to implement the throw mechanism by hand.
The closest mapping is to put a goto at the end of the function, call our destructors,
and return an error code. Caller similarly detects the error code, and execute a goto to
his own error-handler.
As long as you adopt a disciplined approach, it works well, and unlike exception handling
it's not doing anything behind your back.
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2015-01-05 09:39 +1300 |
| Message-ID | <cgtmngF55s4U2@mid.individual.net> |
| In reply to | #55803 |
Malcolm McLean wrote:
> On Sunday, January 4, 2015 7:37:40 AM UTC, Ian Collins wrote:
>> Malcolm McLean wrote:
>>>
>>> Don't invent variables for the sake of avoiding a goto, and certainly
>>> don't use a fake loop with a break just so that you can say the
>>> word "goto" appears nowhere in your source.
>>>
>>> I'd write like this:
>>>
>>> int f(char *x)
>>> {
>>> if(x == NULL)
>>> goto passed_null;
>>>
>>> /* logic here */
>>> if(x == NULL)
>>> goto madex_null;
>>>
>>> return 0;
>>> passed_null:
>>> /* clean up here */
>>> return 1;
>>> madex_null:
>>> /* duplicate clean up */
>>> return 2;
>>> }
>>
>> I assume you are taking the piss?
>>
>> I can't think of any other reason for such gratuitous use of goto.
>>
>>
> The perennial problem is that short code snippets don't illustrate how real code behaves.
>
> Functions should ideally have one point of entry and one point of exit, and the error-handling
> code should be distinguished from the normal flow control.
While that is a common idiom, I can't see any justification for using
goto in precondition tests. Preconditions are just that, they should be
tested before the function does anything else and if they fail, return
an error code immediately. Splitting the check from the return removes
clarity from the code.
> C doesn't make that easy to achieve. C++ allows you to throw an exception. In C, we have
> to implement the throw mechanism by hand.
> The closest mapping is to put a goto at the end of the function, call our destructors,
> and return an error code. Caller similarly detects the error code, and execute a goto to
> his own error-handler.
There are exceptions, but in most cases this style results from a
function trying to do too much. It was easier to justify in days gone
by when compilers weren't very good at inlining functions, not so theses
days.
> As long as you adopt a disciplined approach, it works well, and unlike exception handling
> it's not doing anything behind your back.
Unlike exception handling, it doesn't protect you from an almost
inevitable lapse of discipline! I've had to fix a few too many bugs
cased by missing or incorrect cleanup.
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Jorgen Grahn <grahn+nntp@snipabacken.se> |
|---|---|
| Date | 2015-01-04 22:53 +0000 |
| Message-ID | <slrnmajh3h.4et.grahn+nntp@frailea.sa.invalid> |
| In reply to | #55834 |
On Sun, 2015-01-04, Ian Collins wrote: > Malcolm McLean wrote: ... >> C doesn't make that easy to achieve. C++ allows you to throw an exception. In C, we have >> to implement the throw mechanism by hand. >> The closest mapping is to put a goto at the end of the function, call our destructors, >> and return an error code. Caller similarly detects the error code, and execute a goto to >> his own error-handler. > > There are exceptions, but in most cases this style results from a > function trying to do too much. It was easier to justify in days gone > by when compilers weren't very good at inlining functions, not so theses > days. +1. I cannot easily come up with an example, but IME splitting things carefully into smaller functions also helps making the error handling/resource cleanup more manageable. Sometimes I'm even surprised how well that works. And I believe (helper) functions are still underused in a lot of real code. It's as if people correctly identify the need for a function foo() in their code, but fail to see that foo() itself may benefit from being broken down. Reading the Linux kernel source code taught me a lot about this: they have some nice designs. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o .
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-01-04 01:29 +0000 |
| Message-ID | <87sifr1gge.fsf@bsb.me.uk> |
| In reply to | #55792 |
"S.K.R. de Jong" <SKRdJ@nowhere.net> writes:
> I would like the opinion of people in this forum as to whether or
> not the following is poor style:
>
> int
> f(char *x)
> {
> int diagnostic = 0;
>
> do {
> if (x == NULL)
> break;
>
> ...
>
> diagnostic = 1;
>
> } while(0);
>
> return diagnostic;
> }
>
> The same could be accomplished with goto, of course. Which kind of
> construct is more acceptable for dealing with error conditions: the empty
> while loop? goto with labels? Something else?
Why not:
int f(char *x)
{
if (x == NULL)
return 0;
else {
...
return 1;
}
}
or just
int f(char *x)
{
if (x == NULL)
return 0;
...
return 1;
}
I feel you are not presenting the real issue and you are imagining a
more complex scenario.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Morris Dovey <mrdovey@iedu.com> |
|---|---|
| Date | 2015-01-04 00:00 -0600 |
| Message-ID | <m8akuf$f1r$1@speranza.aioe.org> |
| In reply to | #55792 |
On 1/3/15 6:46 PM, S.K.R. de Jong wrote:
> I would like the opinion of people in this forum as to whether or
> not the following is poor style:
>
> int
> f(char *x)
> {
> int diagnostic = 0;
>
> do {
> if (x == NULL)
> break;
>
> ...
>
> diagnostic = 1;
>
> } while(0);
>
> return diagnostic;
> }
>
> The same could be accomplished with goto, of course. Which kind of
> construct is more acceptable for dealing with error conditions: the empty
> while loop? goto with labels? Something else?
Something else.
int f(char * x)
{ int diagnostic = 0;
if (x != NULL)
{ ...
diagnostic = 1;
}
return diagnostic;
}
--
Morris Dovey
http://www.iedu.com/Solar/
[toc] | [prev] | [next] | [standalone]
| From | Morris Dovey <mrdovey@iedu.com> |
|---|---|
| Date | 2015-01-04 02:20 -0600 |
| Message-ID | <m8at4s$vf2$1@speranza.aioe.org> |
| In reply to | #55798 |
On 1/4/15 12:00 AM, Morris Dovey wrote:
> Something else.
>
> int f(char *x)
> { int diagnostic = 0;
>
> if (x != NULL)
> { ...
> diagnostic = 1;
> }
> return diagnostic;
> }
Or even:
int f(char *x)
{ int diagnostic = !!x;
if (diagnostic)
{ ...
}
return diagnostic;
}
:-)
--
Morris Dovey
http://www.iedu.com/Solar/
[toc] | [prev] | [next] | [standalone]
| From | August Karlstrom <fusionfile@gmail.com> |
|---|---|
| Date | 2015-01-04 15:46 +0100 |
| Message-ID | <m8bjn7$orm$1@speranza.aioe.org> |
| In reply to | #55792 |
On 2015-01-04 01:46, S.K.R. de Jong wrote:
> I would like the opinion of people in this forum as to whether or
> not the following is poor style:
>
> int
> f(char *x)
> {
> int diagnostic = 0;
>
> do {
> if (x == NULL)
> break;
>
> ...
>
> diagnostic = 1;
>
> } while(0);
What's wrong with
while (x != NULL) {
...
diagnostic = 1;
}
?
-- August
[toc] | [prev] | [next] | [standalone]
| From | Ike Naar <ike@iceland.freeshell.org> |
|---|---|
| Date | 2015-01-04 15:10 +0000 |
| Message-ID | <slrn3vfsmailve.l6f.ike@iceland.freeshell.org> |
| In reply to | #55810 |
On 2015-01-04, August Karlstrom <fusionfile@gmail.com> wrote:
> On 2015-01-04 01:46, S.K.R. de Jong wrote:
>> I would like the opinion of people in this forum as to whether or
>> not the following is poor style:
>>
>> int
>> f(char *x)
>> {
>> int diagnostic = 0;
>>
>> do {
>> if (x == NULL)
>> break;
>>
>> ...
>>
>> diagnostic = 1;
>>
>> } while(0);
>
> What's wrong with
>
> while (x != NULL) {
> ...
> diagnostic = 1;
> }
>
> ?
The original do-while loop iterates only once (with a possible
break in the middle of the first -and only- iteration).
[toc] | [prev] | [next] | [standalone]
| From | August Karlstrom <fusionfile@gmail.com> |
|---|---|
| Date | 2015-01-04 17:06 +0100 |
| Message-ID | <m8bodn$5dj$1@speranza.aioe.org> |
| In reply to | #55811 |
On 2015-01-04 16:10, Ike Naar wrote:
> On 2015-01-04, August Karlstrom <fusionfile@gmail.com> wrote:
>> On 2015-01-04 01:46, S.K.R. de Jong wrote:
>>> I would like the opinion of people in this forum as to whether or
>>> not the following is poor style:
>>>
>>> int
>>> f(char *x)
>>> {
>>> int diagnostic = 0;
>>>
>>> do {
>>> if (x == NULL)
>>> break;
>>>
>>> ...
>>>
>>> diagnostic = 1;
>>>
>>> } while(0);
>>
>> What's wrong with
>>
>> while (x != NULL) {
>> ...
>> diagnostic = 1;
>> }
>>
>> ?
>
> The original do-while loop iterates only once (with a possible
> break in the middle of the first -and only- iteration).
>
Ah, I misread the zero as loop forever. Thanks.
-- August
[toc] | [prev] | [next] | [standalone]
| From | Clark Smith <noaddress@nowhere.net> |
|---|---|
| Date | 2015-01-04 15:30 +0000 |
| Message-ID | <m8bmb9$cg3$1@news.albasani.net> |
| In reply to | #55792 |
On Sun, 04 Jan 2015 00:46:44 +0000, S.K.R. de Jong wrote:
> I would like the opinion of people in this forum as to whether or not
> the following is poor style:
>
> int f(char *x)
> {
> int diagnostic = 0;
>
> do {
> if (x == NULL)
> break;
>
> ...
>
> diagnostic = 1;
>
> } while(0);
>
> return diagnostic;
> }
>
> The same could be accomplished with goto, of course. Which kind of
> construct is more acceptable for dealing with error conditions: the
> empty while loop? goto with labels? Something else?
I thought that the ellipsis would convey what I meant, but that
does not seem to have been the case. Here is a nonsensical, toy example
that illustrates better what I have in mind:
int f(char *x, int l)
{
char *a = NULL, *b = NULL;
int diagnostic = 0;
do {
if (x == NULL)
break;
if (l > 255)
break;
a = malloc(256);
if (a == NULL)
break;
b = malloc(2*256);
if (b == NULL)
break;
strcpy(a, x);
strcpy(b, x);
strcpy(&b[255], a);
diagnostic = 1;
} while(0);
if (diagnostic == 0) {
if (a != NULL)
free(a);
if (b != NULL)
free(b);
}
return diagnostic;
}
The idea is that some actions must be taken that must be correctly undone
in case of error. With this approach, everything is centralized at the
end of the loop.
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-01-04 07:44 -0800 |
| Message-ID | <57491de8-6480-47d1-b876-86236262e3da@googlegroups.com> |
| In reply to | #55813 |
On Sunday, January 4, 2015 3:30:58 PM UTC, Clark Smith wrote: > > The idea is that some actions must be taken that must be correctly undone > in case of error. With this approach, everything is centralized at the > end of the loop. > That's basically sensible. Typically a function is constructing some complex data structure. If it encounters an error, the structure is in a half-completed state, so needs deconstructing with care to avoid memory leaks. So you want the code in one place. However just use goto error_exit. Some people have heard that goto isn't good. Whilst the person who made that statement (Dijkstra) was no idiot, repeating him like a mantra isn't very helpful, and requires no thought.
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <invalid@see.sig.invalid> |
|---|---|
| Date | 2015-01-05 00:35 +0000 |
| Message-ID | <Y_kqw.363096$be7.149990@fx14.am4> |
| In reply to | #55815 |
Malcolm McLean wrote: > On Sunday, January 4, 2015 3:30:58 PM UTC, Clark Smith wrote: >> >> The idea is that some actions must be taken that must be correctly undone >> in case of error. With this approach, everything is centralized at the >> end of the loop. >> > That's basically sensible. > > Typically a function is constructing some complex data structure. If > it encounters an error, the structure is in a half-completed state, > so needs deconstructing with care to avoid memory leaks. So you want > the code in one place. > > However just use goto error_exit. Some people have heard that goto > isn't good. Whilst the person who made that statement (Dijkstra) > was no idiot, repeating him like a mantra isn't very helpful, > and requires no thought. Whilst it is certainly true that I've heard that goto isn't good, I actually have personal experience of its not being good. My first programming language was VS/BASIC. In 1982-83, I wrote several programs that, to my inexperienced eye, seemed to me to be very large - hundreds and hundreds of lines of code! These programs were littered with GOTO statements. (It didn't help that I didn't know about GOSUB.) On far too many occasions, I wanted to change the behaviour of a particular program in a particular way, only to find that I simply couldn't do it - the flow of control was just too... well, not to put too fine a point on it, too screwed-up. When I was learning C, at first I was puzzled by the absence of line numbers. How was I going to goto now? But I didn't dwell on it, and before very long I'd adapted to this weird new idea of calling a function to do stuff. By the time I'd learned how goto works, I'd discovered that I just didn't need it. It's an irrelevance. I don't avoid it for religious reasons. In fact, I don't avoid it at all. I just don't use it. There is a difference, which can be illustrated by a list of other things I don't avoid: break-dancing, Val Doonican, lager, Cadillacs, the Large Hadron Collider, Mantovani, and the Metropolitan District of South Humberside. -- Richard Heathfield Email: rjh at cpax dot org dot uk "Usenet is a strange place" - dmr 29 July 1999 Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2015-01-05 01:02 +0000 |
| Message-ID | <Dolqw.330807$fX6.290031@fx26.am4> |
| In reply to | #55859 |
On 05/01/2015 00:35, Richard Heathfield wrote: > Malcolm McLean wrote: > >> On Sunday, January 4, 2015 3:30:58 PM UTC, Clark Smith wrote: >>> >>> The idea is that some actions must be taken that must be correctly undone >>> in case of error. With this approach, everything is centralized at the >>> end of the loop. >>> >> That's basically sensible. >> However just use goto error_exit. Some people have heard that goto >> isn't good. Whilst the person who made that statement (Dijkstra) >> was no idiot, repeating him like a mantra isn't very helpful, >> and requires no thought. > > Whilst it is certainly true that I've heard that goto isn't good, I actually > have personal experience of its not being good. You might, nevertheless, prefer reading other people's code that uses gotos sensibly and consistently, to alternates that involve tortuous logic or where the action keep shifting to dozens of extra functions (with the complicated interfaces required to allow them to share common data). > My first programming > language was VS/BASIC. In 1982-83, I wrote several programs that, to my > inexperienced eye, seemed to me to be very large - hundreds and hundreds of > lines of code! These programs were littered with GOTO statements. The would have a non-sensible use of goto (but typical for Basic at the time). -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <invalid@see.sig.invalid> |
|---|---|
| Date | 2015-01-05 01:43 +0000 |
| Message-ID | <3%lqw.487760$tr6.370454@fx15.am4> |
| In reply to | #55864 |
BartC wrote: > On 05/01/2015 00:35, Richard Heathfield wrote: >> Malcolm McLean wrote: >> >>> On Sunday, January 4, 2015 3:30:58 PM UTC, Clark Smith wrote: >>>> >>>> The idea is that some actions must be taken that must be correctly >>>> undone in case of error. With this approach, everything is centralized >>>> at the end of the loop. >>>> >>> That's basically sensible. > >>> However just use goto error_exit. Some people have heard that goto >>> isn't good. Whilst the person who made that statement (Dijkstra) >>> was no idiot, repeating him like a mantra isn't very helpful, >>> and requires no thought. >> >> Whilst it is certainly true that I've heard that goto isn't good, I >> actually have personal experience of its not being good. > > You might, nevertheless, prefer reading other people's code that uses > gotos sensibly and consistently, to alternates that involve tortuous > logic or where the action keep shifting to dozens of extra functions > (with the complicated interfaces required to allow them to share common > data). In much the same way, I'd prefer to have my leg removed by a surgeon than by an alligator. NEVERTHELESS, keeping the leg is still the best option. -- Richard Heathfield Email: rjh at cpax dot org dot uk "Usenet is a strange place" - dmr 29 July 1999 Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2015-01-05 11:35 +0000 |
| Message-ID | <VFuqw.331090$lM6.328956@fx22.am4> |
| In reply to | #55869 |
On 05/01/2015 01:43, Richard Heathfield wrote: > BartC wrote: > >> On 05/01/2015 00:35, Richard Heathfield wrote: >>> Whilst it is certainly true that I've heard that goto isn't good, I >>> actually have personal experience of its not being good. >> >> You might, nevertheless, prefer reading other people's code that uses >> gotos sensibly and consistently, to alternates that involve tortuous >> logic or where the action keep shifting to dozens of extra functions >> (with the complicated interfaces required to allow them to share common >> data). > > In much the same way, I'd prefer to have my leg removed by a surgeon than by > an alligator. NEVERTHELESS, keeping the leg is still the best option. That's a very glib reply. But read my post again. People trying to avoid goto because of some principle and who end up writing convoluted, more error-prone code in trying to do so is a real problem. -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | raltbos@xs4all.nl (Richard Bos) |
|---|---|
| Date | 2015-01-05 12:58 +0000 |
| Message-ID | <54aa8a20.9386265@news.xs4all.nl> |
| In reply to | #55869 |
Richard Heathfield <invalid@see.sig.invalid> wrote: > BartC wrote: > > On 05/01/2015 00:35, Richard Heathfield wrote: > >> Whilst it is certainly true that I've heard that goto isn't good, I > >> actually have personal experience of its not being good. > > > > You might, nevertheless, prefer reading other people's code that uses > > gotos sensibly and consistently, to alternates that involve tortuous > > logic or where the action keep shifting to dozens of extra functions > > (with the complicated interfaces required to allow them to share common > > data). > > In much the same way, I'd prefer to have my leg removed by a surgeon than by > an alligator. NEVERTHELESS, keeping the leg is still the best option. Myeah, but some people get malignant tumors in their legs, or necrotising fascitis, and amputation might be the only way to save your life. In that case, I'd prefer a neat goto to a dirty use of do(0). Richard
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <invalid@see.sig.invalid> |
|---|---|
| Date | 2015-01-05 13:14 +0000 |
| Message-ID | <Z6wqw.413426$Z36.18436@fx36.am4> |
| In reply to | #55893 |
Richard Bos wrote: > Richard Heathfield <invalid@see.sig.invalid> wrote: > >> BartC wrote: >> > On 05/01/2015 00:35, Richard Heathfield wrote: > >> >> Whilst it is certainly true that I've heard that goto isn't good, I >> >> actually have personal experience of its not being good. >> > >> > You might, nevertheless, prefer reading other people's code that uses >> > gotos sensibly and consistently, to alternates that involve tortuous >> > logic or where the action keep shifting to dozens of extra functions >> > (with the complicated interfaces required to allow them to share common >> > data). >> >> In much the same way, I'd prefer to have my leg removed by a surgeon than >> by an alligator. NEVERTHELESS, keeping the leg is still the best option. > > Myeah, but some people get malignant tumors in their legs, or > necrotising fascitis, and amputation might be the only way to save your > life. In that case, I'd prefer a neat goto to a dirty use of do(0). So would I, but neither is as attractive an option as a neat non-goto non- do/while(0) solution. De gustibus non est disputandum and all that, but I am very satisfied with a style that ignores both these techniques, and which hasn't caused me any problems whatsoever. I suppose that what I think of as a strongly-structured style happens to fit the way that I think about writing programs. Equally clearly, I realise that it isn't everybody's cup of tea. -- Richard Heathfield Email: rjh at cpax dot org dot uk "Usenet is a strange place" - dmr 29 July 1999 Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
Page 1 of 6 [1] 2 3 4 5 6 Next page →
Back to top | Article view | comp.lang.c
csiph-web