Article View: comp.lang.c
Article #606Re: Taming The Switch-Statement
From: David Brown
Date: Tue, 20 Oct 2026 12:58
Date: Tue, 20 Oct 2026 12:58
329 lines
15285 bytes
15285 bytes
On 20/10/2026 13:41, bart wrote:
> 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.)
It is correct that the idea of having a "loop-break" statement was
considered and quickly rejected by people looking at multi-level breaks.
It would be silly, when first making a language change to handle one
type of coding situation, to pick a special case when a more general
solution could be used. I don't think anyone was arguing that the
desire for breaking out of nested loops was /common/, but I don't think
many people felt breaking out of a switch in a loop was common either.
Both needs are relatively rare in general C coding.
But while the consensus is that a "loop-break" statement is not worth
adding to C, that is a completely different thing than claiming that
"everyone" said you were wrong to say nested loop breaks are common!
So - again - please cut the paranoia and exaggerations, and /read/ what
people write, rather than the conspiracies you imagine they write.
>
>>> 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.
Yes, sorry, I should have posted tens of thousands of lines of C code
using these. I'm sure that would have made things much clearer than a
couple of extracted examples.
But let us take a concrete example. These network functions are from
the LWIP network stack, which is designed to be used on small embedded
systems that may not have any kind of OS - so it uses call-backs for
asynchronous programming.
An example API function is this :
err_t dns_gethostbyname(const char *hostname, ip_addr_t *addr,
dns_found_callback found, void *callback_arg)
The returned "err_t" can be "ERR_OK" (indicating the "hostname" is in
the DNS cache and *addr is set to the IP), "ERR_ARG" for argument
errors, or "ERR_INPROGRESS" to indicate that the stack is looking up the
DNS name, and will call the callback when there is a final result or
timeout.
Without typedef's, that function signature would be something like :
signed char dns_gethostbyname(const char *hostname, struct ip_addr
*addr, void (*found)(const char *name, const struct ip_addr
*ipaddr, void *callback_arg), void *callback_arg);
Do you expect anyone to believe you if you claim this is easier to read
than the real declaration using typedefs? I'll believe you if you say
you find the original hard to read - but not if you claim the
"untypedef'ed" version is clearer.
The typedefs let you understand the code piece by piece.
A "dns_found_callback" is a callback function called when a dns lookup
has finished - that's clear from its name. It is called with arguments
the name that was looked up, the ip address that was found, and a
user-supplied extra argument that can be used as you want. That's
simple and clear.
The "dns_gethostbyname" function is a function to find a host's IP
address, given its name - that's clear from the name. It is called with
the name to look up, a pointer to a place to store the resulting IP
address, a callback function of type "dns_found_callback" if it can't
answer immediately, and a user-supplied extra argument for the callback
function. This is again, simple and clear.
>
> 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.)
/C/ does not need the parameter names - /programmers/ need them, because
they tell you want they do. Omitting them is as smart as calling all
your functions "foo1", "foo2", etc., just because C does not require
sensible names.
The point of using typedefs here is that they make the xTimerCreate()
function declaration simpler and clearer, and it tells the programmer
what is going on. It tells the programmer that the parameter is a
TimerCallbackFunction - not just any old function that takes a
TimerHandle_t parameter. The programmer knows that a suitable argument
is their "flash_the_green_led" function, but not their
"initialise_timer" function that happens coincidentally to have the same
signature.
>
> 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.
I am glad I don't have to work with your code - whatever language you
use. The number one skill for programmers is picking good names, and
you flumped that one.
>
>
>> 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.
It is not "hiding a useful fact" - it is abstracting away from an
unintended coincidence. My own preference is to use stronger types,
precisely so that things could not be mixed up. I'd have :
typedef struct { void * arg; } lwip_thread_arg;
typedef void (*lwip_thread_fn)(lwip_thread_arg * arg);
typedef struct { void * arg; } tcpip_init_done_arg;
typedef void (*tcpip_init_done_fn)(tcpip_init_done_arg * arg);
(If I were to go back in time and change things in C, I'd make "typedef"
define new types, not type aliases.)
>
> 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.
You don't need to duplicate the name when it is not helpful - and you
certainly should not do so when it is clearly wrong (a thread function
is not a callback function, even if the signatures match). A real
function signature using this type is :
sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread,
void *arg, int stacksize, int prio);
>
> 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);
/You/ might have written that, but it would be significantly worse code.
You are obsessed with the details of the syntax of the type, instead
of looking at what it represents and what it will mean in the code.
There can be occasional uses for "generic" names, like :
typedef void (*fp_void)(void);
just as there are uses for generic "int" types rather than types like
"size_t", "int32_t", "TickType_t", etc. But using well-named types
conveys vital information. Using badly named types, or re-using named
types inappropriately, makes code worse.
>
> 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.
No, it should not be your main use of typedef - but it might
occasionally be a reason.
>
> 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.
Now you are beginning to understand! Yes, these are two key reasons for
typedefs.
>
> Saving keypresses is bonus benefit, but if it is shorter, then it can
> also help readability as other critical elements are not overshadowed.
I agree - but that is a bonus, not an aim in itself. There is a balance
to be found between being too verbose, and not being explicit enough.
The "right" balance point depends very much on the context, the code,
and the target audience.
>
> So I'm not sure what objection you have to this particular use of it.
I don't object to having shorter aliases when appropriate. I object to
it as a prime motivation.
>
> 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.
I can't see any use in a table of function pointers. I can see a use in
a table of event handlers, a table of timer callback functions, a table
of state machine state handlers, etc.
It's like saying you have an array of "int". No, you have an array of
the days in each month of the year, or an array of timestamps of
keypresses, or an array of the cost of different coffee mugs.
>
> 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).
>
That is why I guessed "host_handler_fn" as a name, but you snipped that.
Technical Details
Message-ID:
<11b7ogt$2m8c9$5@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-mailReferences:
<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> <11b7k22$2v8e5$1@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: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Tue, 20 Oct 2026 14:58:05 +0200 Organization: A noiseless patient Spider Lines: 329 Message-ID: <11b7ogt$2m8c9$5@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> <11b7k22$2v8e5$1@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 12:58:07 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2826633"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+bbvihfpO1reOWrSdl1ZXv7jaCHeyDfgo="; posting-host="6609bebd5817c7daf1cbce2eb44e7728" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:anj6KEbRuSKUJxey9WK/vb1Bdxo= sha256:iUjDPSYhf/8s/q2V5VBKU8+rdBVSFRfTcTQ4Q1mwJnU= sha1:AYhY1FeSFHNoSk3/7pV8YSyfeLw= sha256:90cdNRb0ISjeIqw932EweH+6GDpA05BbnZmiL80AeMc= Content-Language: en-GB, nb-NO In-Reply-To: <11b7k22$2v8e5$1@dont-email.me>