🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #603

Re: Taming The Switch-Statement

#603
From: bart
Date: Tue, 20 Oct 2026 11:41
173 lines
8149 bytes
On 20/10/2026 09:01, David Brown wrote:
> On 19/10/2026 23:28, bart wrote:

>> (Funny that when I suggest that nested loop breaks are uncommon, 
>> everyone says I'm wrong! Some even write 200-line made-up examples to 
>> prove their point.)
> 
> I don't remember "everyone" saying you were wrong to say that nested 
> loop breaks are uncommon - in fact, I don't remember /anyone/ saying 
> that was wrong. 

(There was a LOT of antipathy towards my idea to fix the common case of 
'loop-break' clashing with 'switch-break', BECAUSE it did not fix the 
problem of nested loop-break, which is uncommon, even though that wasn't 
the aim.)

>> There is no reason to abstract away that function pointer type.
>>
>> You probably do so in C /because/ the type involves * () and [] but 
>> have somehow persuaded yourself that it just better that way.
> 
> Do you hear that whooshing noise?  It is the sound of the point flying 
> far above your head.
> 
> Here is an example of some sensible function typedefs, all from third- 
> party code (so not my "peculiar" style)
> 

> typedef void (*TimerCallbackFunction_t)( TimerHandle_t xTimer );
> 
> TimerHandle_t xTimerCreate(  const char * const pcTimerName,
>                   TickType_t xTimerPeriodInTicks,
>                   UBaseType_t uxAutoReload,
>                   void * pvTimerID,
>                   TimerCallbackFunction_t pxCallbackFunction );
> 

> typedef void (*lwip_thread_fn)(void *arg);
> typedef void (*tcpip_init_done_fn)(void *arg);
> typedef void (*tcpip_callback_fn)(void *ctx);
> typedef void (* sys_timeout_handler)(void *arg);
> typedef void (*dns_found_callback)(const char *name,
>                   const ip_addr_t *ipaddr, void *callback_arg);


This is the kind of C I find hard to read. There are lots of typedefs 
but few uses of those typedefs that I can see, so I don't know how much 
difference they make.

Except here:

  typedef void (*TimerCallbackFunction_t)( TimerHandle_t xTimer );

  TimerHandle_t xTimerCreate(  const char * const pcTimerName,
                    TickType_t xTimerPeriodInTicks,
                    UBaseType_t uxAutoReload,
                    void * pvTimerID,
                    TimerCallbackFunction_t pxCallbackFunction );

Without the typedef, I believe it would just be this:

TimerHandle_t xTimerCreate(  const char * const pcTimerName,
                  TickType_t xTimerPeriodInTicks,
                  UBaseType_t uxAutoReload,
                  void * pvTimerID,
                  void (*pxCallbackFunction)(TimerHandle_t));

(I've left out the parameter name of the callback function, as I don't 
believe it is that useful.

I've left in the regular parameter names, even though C doesn't need 
them, but they are quite heavy - you don't really need those 
Hungarian-style prefixes.)

Well, I guess this buys you one thing: the parameter name for the last 
parameter is to the right of its type, rather than be inside it. But a C 
expert should not be fazed by that.

Here is the same function as I might write it in an alternative syntax.

I've kept the same names, but changed case-style and removed prefixes 
and suffixes. I've capitalised user-type names:

func TimerCreate(                 # (retain case if FFI import)
     ichar     timername,
     Ticktype  timerperiodinticks,
     Ubasetype autoreload,
     ref void  timerid,
     ref proc(Timerhandle) callbackfn)

In this form, it's hard to see what benefit a typedef would give for 
that last parameter; it is simple enough.


> Ask yourself why the typedefs are used.  Look at the /names/.  Those 
> last four are all from the same network stack library, and are all 
> typedefs for "void (*)(void*)".

Until you mentioned it, I hadn't noticed. So typedef has done a good job 
of hiding that useful fact! But looking more closely, it is the first 
four of the last five.

Let's take one:

  typedef void (*lwip_thread_fn)(void *arg);

That 'arg' is not much use for a start (other than being a hint that 
this particular (...) is where the parameters go).

So, maybe this is used like this:

   void F(lwip_thread_fn lwip_thread_callback);

Here you have two identifiers imparting the sort kind of information; a 
tautology.

If that function pointer pattern is common, or recurs in a suite of 
functions, I might also have used a typedef, but with a common name. 
Then only one would be needed, say:

  typedef void (*stack_callbackfn)(void);

I might have done the same in that alternate syntax as it would start to 
get cluttery: 'ref proc(ref void)', but also in order to define that 
common pattern in one place.

>> Note that 'pcl' in my example /is/ a typedef; here it makes sense as 
>> it is used in 100s of places. This function pointer occurs in only one 
>> place.
> 
> I'm sorry, I had mistaken you for a programmer who wanted to write good, 
> clear programs.  Naming things is not a shortcut to save keypresses.

Isn't that one of the main uses of typedefs? So as to give a handy, 
'euphonious' one-token alias to an elaborate typedef.

It also allows you to change the type in one place without having to 
update hundreds of instances.

It also allows the type to be opaque.

Saving keypresses is bonus benefit, but if it is shorter, then it can 
also help readability as other critical elements are not overshadowed.

So I'm not sure what objection you have to this particular use of it.

Here is the one place I do remember using typedef for a function pointer 
(from an old project, not in C):

     type fnptr = ref proc

     repeat
         (fnptr(pcptr^))^()
     until stopped

It's used as a cast, but reason I have it is because with simple cast 
syntax, which is 'T(X)', the meaning without it would be ambiguous (a 
grammar issue that I might fix one day).

Rather than fallback to standard cast syntax (which is 'cast(X, T)'), 
this was tidier.

>> BTW what name would be given to such a function pointer type?

> I have no idea what your code is supposed to do,

It's a table of function pointers! All sharing the same signature.

That's pretty generic. Is it really that hard to guess? You don't need 
to know what they do, other than at some point they will be called as 
handlertable[i](arg).

Technical Details
Message-ID: <11b7k22$2v8e5$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <119qf6l$2rmg3$1@dont-email.me> <11arjmc$2qid7$1@dont-email.me> <11at5vi$5vu$1@reader2.panix.com> <11auad0$3no1k$1@dont-email.me> <11aupfl$3ul$1@reader2.panix.com> <11avmfb$55rt$1@dont-email.me> <11b2a6d$131h6$1@dont-email.me> <11b2hr6$15thq$1@dont-email.me> <11b2l06$16mof$1@dont-email.me> <11b2qv0$19h68$1@dont-email.me> <11b35gt$1cn63$2@dont-email.me> <11b557o$1kr9u$1@dont-email.me> <11b56uo$1q18k$5@dont-email.me> <11b6224$2ejik$1@dont-email.me> <11b774o$2m8c9$3@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: bart <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Tue, 20 Oct 2026 12:41:55 +0100
Organization: A noiseless patient Spider
Lines: 173
Message-ID: <11b7k22$2v8e5$1@dont-email.me>
References: <119qf6l$2rmg3$1@dont-email.me> <11arjmc$2qid7$1@dont-email.me>
 <11at5vi$5vu$1@reader2.panix.com> <11auad0$3no1k$1@dont-email.me>
 <11aupfl$3ul$1@reader2.panix.com> <11avmfb$55rt$1@dont-email.me>
 <11b2a6d$131h6$1@dont-email.me> <11b2hr6$15thq$1@dont-email.me>
 <11b2l06$16mof$1@dont-email.me> <11b2qv0$19h68$1@dont-email.me>
 <11b35gt$1cn63$2@dont-email.me> <11b557o$1kr9u$1@dont-email.me>
 <11b56uo$1q18k$5@dont-email.me> <11b6224$2ejik$1@dont-email.me>
 <11b774o$2m8c9$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 20 Oct 2026 11:41:55 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3121605"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+g5MTMLCvW4MRCUk3QBbKv"; posting-host="7c142682e3246f1a229ef2ca48617714"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:4n259DTkk+nFqIEetLMEDPA2WGY= sha256:IArO19q88JozLsAr0fs+zj0o1ao0ez4uGRWqdQ6/88k=
	sha1:waIrhxOhc8FKcs1shFTGIBqBtuw= sha256:bzoYZfjjbu702ZDWl5jk80RATXHtYXKECbbJP8Zb67s=
Content-Language: en-GB
In-Reply-To: <11b774o$2m8c9$3@dont-email.me>