🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #212

Re: Taming The Switch-Statement

#212
From: David Brown
Date: Sun, 11 Oct 2026 12:42
120 lines
7068 bytes
On 11/10/2026 12:58, bart wrote:
> On 10/10/2026 16:50, David Brown wrote:
>> 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.
> 
> We don't know that, we just see the end result.

I don't think there is anything much you can say about that without 
sidetracking to a definition of what we mean by "a problem", and then 
doing a massive survey of lots of C programmers.

We do know that C is a massively successful programming language, used 
by vast numbers of people, and that switch statements are common in C. 
This is true regardless of any real or imagined "problem" with C's 
switch statements.

> 
>>   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.
> 
> This is the core C code for Duff's Device from Wikipedia:
> 

We know what Duff's Device is, and no one cares about your language.  It 
is not an alternative to C in any way.

>> 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.
> 
> That is astounding. But then, how many other languages, which have 
> copied all sorts of dubious features from C, have also duplicated its 
> bizarre switch statement?
> 
> That should tell you more.

We could pick a dozen different real-world languages, with a dozen 
different kinds of switch, case, match or other multiple choice 
construct (or lack thereof).  That should tell you that there is no 
"right" or "wrong" choice here - there are different design choices, 
with different pros and cons, different flexibilities and limitations, 
and appropriate for different kinds of language and different kinds of 
tools.

> 
>> 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,
> 
> I suggest you try the same exercise I did.

I suggest you find something better to spend your time on than re-hashes 
of your obsessions about what you don't like about C.

> 
>   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. 
> 
> I posted a proposal for such a statement in a reply to Keith Thompson, 
> who completely dismissed it.
> 

Keith is a big boy, and responsible for his own opinions and posts - it 
is not my place to defend his writing.  However, I entirely agree with 
him in that post.  It would be possible to add a more structured 
switch-like statement to C, without fall-through, but doing so would be 
detrimental to the language as a whole as the disadvantages and added 
complexity would not be balanced by significant added usability or let 
you write code in a significantly better way.
Technical Details
Message-ID: <11ag08f$2k9do$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> <11admri$1s1lb$1@dont-email.me> <11afq4b$2idte$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: Sun, 11 Oct 2026 14:42:55 +0200
Organization: A noiseless patient Spider
Lines: 120
Message-ID: <11ag08f$2k9do$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> <11admri$1s1lb$1@dont-email.me>
 <11afq4b$2idte$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 11 Oct 2026 12:42:56 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2762168"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19bSJsz2bogIMi9rBrG2TdslwclruyaSRw="; posting-host="10283a9df1f0548a34352f02c4ddf11b"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:y02UfsqEn5/gVIDqMkGGdtnh42k= sha256:w+6aCFVoQMxYu3pRV1Q6Nz4mzxbVFZCUP5G1gEGy06w=
	sha1:FidmeCndLMGceqfLAQLpditS4TI= sha256:TRwslvu5WBdtiJSJCK+huf7+wWUjNdZMIef8k4h2uu0=
Content-Language: en-GB
In-Reply-To: <11afq4b$2idte$1@dont-email.me>