Article View: comp.lang.c
Article #599Re: Taming The Switch-Statement
From: David Brown
Date: Tue, 20 Oct 2026 08:01
Date: Tue, 20 Oct 2026 08:01
154 lines
8439 bytes
8439 bytes
On 19/10/2026 23:28, bart wrote:
> On 19/10/2026 14:46, David Brown wrote:
>> On 19/10/2026 15:16, Janis Papanagnou wrote:
>>> On 2026-10-18 21:09, David Brown wrote:
>>>> On 18/10/2026 18:09, bart wrote:
>>>>> [...]
>>> [ declaration syntax ]
>>>
>>> (I just want to comment on a [not unimportant] detail...)
>>>
>>>>
>>>> You need to give things in the right order, or use parentheses, to
>>>> get the right "type expression". It's just like any other kind of
>>>> expression.
>>>>
>>>> The intention of the declaration syntax in C is that the declaration
>>>> order follows usage. [...]
>>>
>>> I think the first part is downplaying that languages typically indeed
>>> make a distinction between parenthesized expressions and declarations.
>>> Quite some other languages [not from the C-heritage] that I used all
>>> did not need to introduce "expression"-grouping for _declarations_ in
>>> the first place given their more consistent ways to declare entities.
>>>
>>> The second part is, as I had understood C's history, the idea behind
>>> how it got defined; needless to say that I think that was a bad idea.
>>> But that's what we've got! If they had dropped that requirement/idea
>>> the declaration syntax could have become "better" (more consistent,
>>> less cryptic, less convoluted, clearer). Now, after half a century,
>>> we just have to live with that, and folks who have "problems" with it
>>> should better not touch this language (or maybe not doing programming
>>> at all). The somewhat tragic aspect is that the "declaration follows
>>> use" idea was (IIRC) intended to _simplify_ the understanding of its
>>> concepts and use of the language; the consequences were seemingly not
>>> valued correctly.
>>>
>> There are a number of things that I don't particularly like about C's
>> declaration syntax, or where I think it would have been better if it
>> were different. But the "inside-to-outside" ordering is not one of
>> them. That is not because I think it is better or clearer than left-
>> to- right ordering, but because when I compare to, say, Pascal, it's
>> not a big issue for me. Far bigger are :
>>
>> 1. People put too much into a single type declaration, rather than
>> building types out of logical parts with a purpose and a name. They
>> don't do it as much as Bart likes to pretend, but some people do it
>> too much for my liking. The language doesn't force you to do that -
>> it's programmer style. IMHO there is never a reason for a "pointer to
>> an array of function pointers taking int and returning void" - no
>> matter what syntax is used.
>
> Then you have a peculiar and restricted coding style.
I don't believe so, no - at least, not compared to other code in my
field. It /is/ restricted, but so is everyone's coding style.
>
> (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. A few people have suggested that they personally would
often find some kind of multi-level break useful, but I do not believe
many people think it is common, as a fraction of all C code they write.
(You really should cut down on the paranoia and the exaggerations.)
>
> These are two actual function pointer arrays from my projects (not C):
>
> []ref proc hosthandlers = ...
>
> [pclnames.len]ref proc(pcl) px_handlertable
>
> 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)
struct tmrTimerControl;
typedef struct tmrTimerControl * TimerHandle_t;
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 (*TaskFunction_t)( void * );
typedef BaseType_t (*TaskHookFunction_t)( void * );
typedef void (*altcp_err_fn)(void *arg, err_t err);
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);
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*)". That function type could easily have
been written out in full in each declaration that used them. Yet the
actual functions using them specifically choose to use the type names
that are relevant at the time.
Do you really need me to spoon-feed you the reasons why this is all a
good idea? People name types because it makes their code clearer, more
robust, more flexible, more portable.
>
> 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.
>
> (The C version looks like: 'void (*name[N]))T)'. I used a tool and
> transcribed it, then I double-checked and found I'd written '(*name)[N]'
> not '(*name[N])'; I wouldn't have noticed.)
>
> BTW what name would be given to such a function pointer type? There is
> nothing obvious other than 'funcptr', but suppose there is more than one
> in scope?
I have no idea what your code is supposed to do, so I have no idea what
names would be good. Presumably you /do/ know what it is for - such as
what "pcl" means - so /you/ pick good names. Maybe "host_handler_fn" ?
>
> Something else strikes me about breaking up a complex type-decl sequence
> into a chain of typedefs: if you take that list of typedefs, and rotate
> it 90 degrees c/w, you have something that resembles the LTR
> representation!
>
If the details of the declaration syntax matter, you are doing it wrong.
Technical Details
Message-ID:
<11b774o$2m8c9$3@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>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 10:01:28 +0200 Organization: A noiseless patient Spider Lines: 154 Message-ID: <11b774o$2m8c9$3@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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 20 Oct 2026 08:01:28 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2826633"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18chFPkU5//diIdUZjesgWATHzK4afsAfk="; posting-host="6609bebd5817c7daf1cbce2eb44e7728" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:wkaZic1R57VeJ4XYCGq47Cbdmv0= sha256:MoALuIUSvWkbn5kPxRhGtbRLF08rVM9maKtpr9Yzt5A= sha1:EmwaYrg+88q4lZdRXCi9AiNeZyQ= sha256:7hOqWOq1Tb+sjcJuyyugyB1gCIjsogHUfIpRsVMKc80= Content-Language: en-GB, nb-NO In-Reply-To: <11b6224$2ejik$1@dont-email.me>