groff
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [groff] Duff's Device lurking in refer.cpp


From: G. Branden Robinson
Subject: Re: [groff] Duff's Device lurking in refer.cpp
Date: Wed, 27 Jun 2018 09:16:18 -0400
User-agent: NeoMutt/20170113 (1.7.2)

At 2018-06-26T23:04:28+0200, Steffen Nurpmeso wrote:
> G. Branden Robinson wrote in <address@hidden
> beast.net>:
>  |I enjoyed this Easter egg; maybe you will too.
> 
> Why easter egg if i might ask?

Using case labels as labels for de facto gotos from a different scope is
surprising to me personally and, I would argue, non-idiomatic C[1][2].

But I brought it up because it amused me.  I'm in the midst of preparing
a commit to make sure all the groff man pages have Synopsis sections
that (1) use .SY and .YS (and .OP where feasible), (2) actually document
the options, and (3) agree with actual command behavior (modulus a few
deliberately undocumented and/or no-op flags), and I found this example
noteworthy because I had to read it carefully to make sure I understood
what I saw.  And then I thought, perhaps wrongly to your and Ralph's
view, "oh, the old Duff's Device trick!".

> I often use this kind of code and am very thankful that C allows
> this.

Yes, I don't and I'm not.  :)

> I would not exactly call it easter egg, if you would
> program this in assembler it would look much more natural than
> here with the brace groups.

Yes, but as the saying goes, you can write Fortran in any language.

> But interesting that you call it Duff's device, i thought that
> exactly refers to a do..while() loop within a switch!, as in

At 2018-06-27T13:04:08+0100, Ralph Corderoy wrote:
> Hi Steffen,
> 
> > >     if (strcmp(opt,"-version")==0) {
> > >     case 'v': // <-- a gibbon on the syntax tree
> >
> > But interesting that you call it Duff's device, i thought that
> > exactly refers to a do..while() loop within a switch
> 
> Agreed, it's a method of loop unrolling that used case labels against
> the `flow' of the other code's structure.
>     dict -d jargon "duff's device"

Yes, and I happily contest this point.  Loop unrolling was Duff's
motivation, but the unrolled loop is not what readers find surprising
about the Device.  What people find surprising is the interpenetration
of the two control structures.  Everywhere else[3] in C, control
structures are more strictly nested.

To emphasize, the sine qua non of Duff's Device is not the loop
unrolling, but the introduction of a do-while{} amidst a series of case
labels.  Hence my comment "a gibbon on the syntax tree" which I added to
the listing for the email.  Were it not for that twist, Duff's Device
would not have earned its fame.

Regards,
Branden

[1] Maybe when teaching C we should be more honest.  Case labels
end with colons because they're labels for implicit gotos.  And they can
only take integer expressions because they're _computed_ gotos--except
that term doesn't mean anything to kids today.  :)

[B] Here I must confess that I am youngster and did not start learning C
until a few years into the ANSI C era, using gcc on Sun machines.  So
K&R-style unprototyped functions seem archaic to me, but real boolean
and complex types feel bracing and fresh.

[C] You could call this generalization "giving a hostage to fortune", or
an invitation to post other examples not obviously drawn from the IOCCC.

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]