Article View: comp.lang.c
Article #644Re: Taming The Switch-Statement
From: David Brown
Date: Wed, 21 Oct 2026 13:10
Date: Wed, 21 Oct 2026 13:10
184 lines
7943 bytes
7943 bytes
On 21/10/2026 00:43, bart wrote:
> On 20/10/2026 22:44, Dan Cross wrote:
>> In article <11b8j17$3btus$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>>> On 20/10/2026 20:24, Dan Cross wrote:
>>>> In article <11b7k22$2v8e5$1@dont-email.me>, bart <bc@freeuk.com>
>>>> wrote:
>>>
>>>> The point is that the name you chose is meaningless to anyone
>>>> other than you. What's a PCL? Phase Control Line? Printer
>>>> Command Language? Photon Compressor Loop?
>>>
>>> Within the context of that application then it would be clear. You're
>>> just being overly hostile.
>>
>> Not really. You are, I presume, posting that code to try and
>> make some kind of point about how "readable" it is. But it's
>> not readable unless you already know what it does.
>
> What it does not relevant. This is simply defining an array of T. But
> because T happens to be a function pointer, DB insists that an alias
> must be created, and a new name invented.
I did not "insist" or say that an alias "must" be created. I said
that's normally what you should do in real code, to make it clear,
readable and maintainable.
Note that I wrote (in regard to the "pcl" stuff) :
"""
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" ?
"""
If "pcl" makes sense in the context of your code, that's fine - use it
in your names.
But don't use generic names where specific names impart useful
information. Don't re-use generic names because of a coincidence in the
underlying implementation - use names that make sense where they are used.
>
> Why? Probably because in C, the syntax for function pointers gets hairy.
No, this is all because I (and Dan) know how to write good code in a
clear (to appropriate readers) and maintainable manner. The syntax is
irrelevant. We appreciate that you find C's declaration syntax
confusing and difficult - I think that after the first two or three
hundred repetitions, it finally sank in. But please stop projecting
your weaknesses and prejudices onto other people. I do not name
function pointer types because of C's syntax - I name them because the
code is better in many ways when the types are named. I would do the
same in Pascal, Rust, Ada, or any other language, regardless of the syntax.
>
>> On the other hand, the code David posted _is_ readable, because
>> it uses the language to encode domain-specific information into
>> type names.
>
> Maybe we should create aliases for function signatures more often then,
> then we can encode that extra info.
>
> So that instead of, for example:
>
> void SomeFunc(int x, int y) {printf("%d %d\n", x, y);}
>
> you do:
>
> typedef void Take2Ints(int x, int y);
>
> Take2Ints SomeFunc {printf("%d %d\n", x, y);}
>
Maybe we should not. That is not helpful to code clarity. It is a
pointless generic name (showing that you /still/ do not understand the
fundamental matter of giving useful and informative names), and instead
of /giving/ useful additional information, it /hides/ details at the
point in the code where they are vital.
Now, if you had a concept of "Coordinate handler" that was expected to
receive an x, y pair and do something with them, then it is possible
that you would have a header file "coordinate_handlers.h" with :
typedef void Coordinate_Handler_Func(int x, int y);
and some related function declarations :
Coordinate_Handler_Func dummy_handler;
Coordinate_Handler_Func simple_printer;
Coordinate_Handler_Func fly_to_new_coordinates;
You are putting the emphasis here on what sort of usage you expect for
these functions, with the assumption that code using them knows how to
call them. This could be handy if you have another module that needs to
build an array or other data structure with these functions - it does
not care about the implementation details, and uses the function type
(or a pointer to the function type) without looking at its contents:
#include "coordinate_handlers.h"
size_t build_coordinate_handler_array(
Coordinate_Handler_Func * hs, size_t max_handlers)
{
assert(max_handlers >= 3);
hs[0] = &dummy_handler;
hs[1] = &simple_printer;
hs[2] = &fly_to_new_coordinates;
return 3;
}
A caller function might have :
#include "coordinate_handlers.h"
void call_handler(size_t handler_no, int x, int y) {
assert(handler_no < no_of_handlers);
Coordinate_Handler_Func h * handlers[handler_no];
h(x, y);
}
Handler implementations would be written normally :
#include "coordinate_handlers.h"
void simple_printer(int x, int y) {
printf("%d %d\n, x, y);
}
Then later when you decide to go to 3 dimensions, you need to change
code that calls these handlers, and you need to change the handler
implementations (and you'll get compile-time errors until you do), but
generic "in the middle" code like "build_coordinate_handler_array" is
unchanged.
But if the code also happened to need lots of functions for drawing
titles at x, y coordinates, it would have :
typedef void Title_Drawer_Func(int x, int y);
You would /not/ re-use the same typedef, just because the implementation
is the same, when the /purpose/ is different.
> If you have a suite of functions sharing the signature, then it might be
> a good idea. But funnily enough, you never see this being done.
>
Given that it is not remotely close to valid C, I don't think it is
particularly surprising.
> (I don't know if this is legal C; but it works fine with my compiler:
Your compiler is not a C compiler - it's a compiler for an undefined and
undocumented superset of an undefined and undocumented subset of an
undocumented version of C. If that's all you need, there's nothing
wrong with that - but what works or does not work with your compiler is
no indication of what is or is not "legal" C.
>
> c:\m>type fred.c
> #include <stdio.h>
>
> typedef void Take2Ints(int x, int y);
>
> Take2Ints SomeFunc{printf("%d %d\n", x, y);}
>
> int main(void) { SomeFunc(10, 20); }
>
> c:\m>bcc -r -q fred
> 10 20
>
> )
>
>> Yeah, you really just don't get it.
> One of us doesn't get it.
Exactly. I'm fairly sure you are alone here.
Technical Details
Message-ID:
<11badkg$3uqp4$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-mailReferences:
<119qf6l$2rmg3$1@dont-email.me> <11b7k22$2v8e5$1@dont-email.me> <11b8f5h$67l$1@reader2.panix.com> <11b8j17$3btus$1@dont-email.me> <11b8nc4$9aq$1@reader2.panix.com> <11b8qqg$3ejlp$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: Wed, 21 Oct 2026 15:10:40 +0200 Organization: A noiseless patient Spider Lines: 184 Message-ID: <11badkg$3uqp4$1@dont-email.me> References: <119qf6l$2rmg3$1@dont-email.me> <11b7k22$2v8e5$1@dont-email.me> <11b8f5h$67l$1@reader2.panix.com> <11b8j17$3btus$1@dont-email.me> <11b8nc4$9aq$1@reader2.panix.com> <11b8qqg$3ejlp$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Wed, 21 Oct 2026 13:10:41 +0000 (UTC) Injection-Info: dont-email.me; logging-data="4156196"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Beldn0aC7XBOTjWjNB6lHrRbdwQRfOD8="; posting-host="f78456a6491f23b3f53847b5a3287905" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:eIO88YkNCucut+RSQpUq21Bso/Y= sha256:nQUs+L8y8dQluJSNFztFDzaCoxHhxWwX7sXlFuZGCmk= sha1:O/g7cS6IYt1d35Pk+EsB+o3eLVQ= sha256:2mugPMjA8xEHs7Z/W5G+89BCLvF+s6pl5kr6hd/gWnw= In-Reply-To: <11b8qqg$3ejlp$1@dont-email.me> Content-Language: en-GB, nb-NO