🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #111

Re: Taming The Switch-Statement

#111
From: David Brown
Date: Sat, 10 Oct 2026 15:50
92 lines
6103 bytes
On 10/10/2026 15:11, bart wrote:
> On 10/10/2026 13:01, Janis Papanagnou wrote:
>> On 2026-10-10 12:53, bart wrote:
>>> On 10/10/2026 03:17, Keith Thompson wrote:
>> [...]
> 
>> I think the problem with your statement is also that you think your
>> preferred construct is the only one that is "right". (You seem to
>> be confident that supporting "fall-though" is generally "wrong".)
> 
> Yes. Why, you think that fall-through is right AND should be the default 
> behaviour?
> 
> If you look at actual C codebases, fall-through is incredibly rare. 
> Nearly always, it will use break, or sometimes other control-flow such 
> as return or goto, so that fall-through doesn't happen.
> 
Keith explained the background for C's switch.  It has its origins in a 
time when "goto" was commonly used, and it is a significant improvement 
on many uses of calculated goto.  It is not "bizarre", dangerous, or 
"dumb" - or "wrong".  It is simple /different/ from the structured 
switch/case/match construct found in many other languages.  The choice 
was made long ago, and it must be obvious to everyone that the behaviour 
could not be changed.

This makes it flexible - you can do things with C's "switch" that you 
cannot do with Pascal "case".  That means you can do some different, 
interesting and useful things - and it also means you can make some 
different and annoying mistakes.

You point out that most C code uses "switch" in a structured manner, 
with "break" rather than implicit fall-through.  That is my experience 
too.  What does that tell us?  I think it tells us two things :

1. People don't have a problem writing structured code with C's switch 
statements, using "break" as appropriate.  It works fine for that purpose.

It's fair, I think, to say that forgetting a "break" is a source of 
error found in C programming but not in languages with a more structured 
equivalent construct - but it is an error that is easily found by common 
tools, and will normally be found in testing otherwise.  "Missing break" 
bugs do make it into the wild sometimes, but I could not find evidence 
that it ranks high in the statistics.

2. C programmers would mostly also have been fine with a structured 
"switch" where each "case" had its own block and there was no 
fall-through.  There are, however, occasional exceptions and more 
complicated code.  Such use-cases would need to be handled in other ways 
if C's switch did not work as it does.  My gut feeling is that a solid 
proportion of such cases were more relevant in the old days before more 
optimising compilers - things like Duff's Device spring to mind as being 
useful long ago, but unnecessary with modern tools.



I personally would have been happier if C's switch was more structured 
and had no fall-through (obviously it would still need a way to have 
multiple cases attached to the same sub-block).  Does that mean I think 
C's switch is "wrong", or a bad design?  No, it does not.  Even if I did 
think it was wrong, would that mean that it /is/ wrong?  No, it would 
not - an opinion is not a fact.

Like a great many things in life, we can simply say that C's "switch" 
might have been a little more convenient if it had been defined in a 
different manner, and when making a new switch-like concept in a new 
language we would probably make different choices.  But it is what it 
is, and it's not hard to use it for correct code, so why make a fuss 
about it?  No amount of wailing and gnashing of teeth here is going to 
change anything.


To re-enforce another of Keith's points, it /would/ be possible to add a 
new switch-like construct to C with more structure and no fall-through. 
But it would provide very little benefit, and simply make the language 
that little bit more complicated - existing "switch" would obviously not 
be removed.

It's a different matter for other languages.  In C++, there is a lot 
that can be gained from a more powerful pattern-matching construct, so 
there is a lot of work going on to add it to the language.  But that is 
not a replacement for "switch" - it is primarily targeting class 
hierarchies (match on different types in the hierarchy), variants (safe 
union types, also known as "summation" types), and other features that 
do not have correspondence with C.  In C++ there is a lot to be gained 
here by a more powerful construct - the lack of implicit fall-through is 
a very minor detail.  For C, there would not be enough benefit - maybe 
it could be used to replace _Generic for some uses, but that's not worth 
the effort.



Technical Details
Message-ID: <11admri$1s1lb$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> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11ad9fg$cjm0$4@dont-email.me> <11addhr$1oakb$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: Sat, 10 Oct 2026 17:50:09 +0200
Organization: A noiseless patient Spider
Lines: 92
Message-ID: <11admri$1s1lb$1@dont-email.me>
References: <119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me>
 <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org>
 <11ad5eq$1l8hg$1@dont-email.me> <11ad9fg$cjm0$4@dont-email.me>
 <11addhr$1oakb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 10 Oct 2026 15:50:10 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1967787"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18/Dw7xSdslOKv48PHdLtiOVLUHZ2U3rAI="; posting-host="626ee86dcd3e34a64f3bc1d5e41ae662"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Kvuhd1ixn9isH2jW8DyHPVNfE4g= sha256:zyuXqhn6FzeQjgC7GkbtMzaYopmOsND9lqiUR08xuZI=
	sha1:9aNmRW3NHOovTId0Lcx+2FqUtzk= sha256:7gOHQV4f1/8jn0a67xJgNVVSAQNplcitrQL2K1/IBvE=
In-Reply-To: <11addhr$1oakb$1@dont-email.me>
Content-Language: en-GB