🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #29

Re: Taming The Switch-Statement

#29
From: Janis Papanagnou
Date: Wed, 07 Oct 2026 13:16
248 lines
11689 bytes
On 2026-10-07 10:28, David Brown wrote:
> On 06/10/2026 19:25, Janis Papanagnou wrote:
>> [...]
> [...]
> 
> If you are getting lots of warnings, you are doing something wrong. 

I don't think this is generally true. (I mentioned already that it
depends on a couple factors.)

(I snip your subsequent explanations because what you intended to
point out in that respect had been clear already.)

> [...]
> 
> The standard for most development, IME, is that completed programs 
> should build without warnings being generated. 

Yes. That had always been our line in our commercial development.
(The build-settings were centralized, though, and far from all
warnings had been activated; the annoyances and problems with of
many unnecessary "hints" were seriously considered. Another poster
here named the negative effect (that I already explained) before as
“confirmation fatigue”. I think we shouldn't underestimate that.)
And individual programmers have not directly created the delivered
programs, though; there were quite some more stages in the process.

A zero-warnings policy is also a landmark in my private context.
But that doesn't mean that I'd activate a full-warnings setting
and go to study all what the diagnostics think might be a problem.
(It may, what I suspected, be a personal thing, based on factors
already mentioned.)

> But that is not because 
> the warnings are swept under the carpet by disabling them because the 
> developer considers himself / herself an expert!

Personal self-valuation was neither the topic nor helpful in real
situations (professional or private). - We should dismiss that idea.

> 
>>>
>>> Some kinds of warnings are more likely to be triggered by people new 
>>> to the language, certainly.  But that does not mean more experienced 
>>> people should not enable those same warnings - it just means they are 
>>> less likely to be triggered. 
>>
>> It might be that we have different situations in mind. - Just take the
>> two of the recently discussed examples, fall-through and shadowing, for
>> example; if I'm using fall-through deliberately I certainly don't want
>> to see a distracting warning, or someone who thinks that shadowing is
>> an oversight in language design (as opposed to a deliberate sensible
>> design as consequence of block-structures and scope) I certainly don't
>> want to see that.
> 
> Let me answer these two example cases, for demonstrative purposes.
> 
>      switch (a) {
>          case 1 :
>              do_something();
>          case 2 :
>              do_something_more();
>              break;
>          case 3:
>              do_something_else;
>      }
> 
>      switch (b) {
>          case 1 :
>              do_something();
>              // fallthrough
>          case 2 :
>              do_something_more();
>              break;
>          case 3:
>              do_something_else;
>      }
> 

(I assume you unintentionally forgot the '()' in "case 3".)

> 
> Which of these would you say is the better code - switch (a) or switch 
> (b) - which both do exactly the same thing?

In the early 1990's, when I co-authored our C/C++ coding standards,
a "fall-through" comment was mandated!

(And, BTW, we also mandated a 'default' branch, if only to write a
diagnostic message to a logfile.)

> 
> That's a rhetorical question - obviously the version the version that 
> makes it clear to readers that the fall-through is intentional is 
> better.  (For C23, "[[fallthrough]];" is an alternative to the comment.)

(We didn't have that C23 feature back then.)

> 
> So use -Wimplicit-fallthrough, write your code clearly so that 
> intentional fall-throughs are obvious, and let your compiler help when 
> you get things wrong.

Does that warning-setting parse existing fall-through comments and
will stay silent if existing? - If not then the point about annoying
warnings that hide real issues still stands. - Okay, I just tried it
with a contemporary gcc; and I see that this modern compiler indeed
parses an existing comment and accepts some strings (not a sloppy
"fal thru", but at least some more accurately written comments). -
That's indeed a useful addition! - (And if we'd have had support for
that in our compilers back then, then we'd certainly have had such a
warning flag in our standard build-scripts since it would serve the
finding of unintentional errors before the reviews-process-stage.)

> 
> The next one is shadowing.  That's an example of a warning that is more 
> a matter of personal style.  Use "-Wshadow" (or "-Wshadow=global") if 
> you want to avoid any kind of shadowing.  Use "-Wshadow=local" if you 
> don't mind shadowing "global" identifiers, but want to avoid local 
> variables shadowing each other.  Don't enable "-Wshadow" if you 
> regularly shadow identifiers.

No, shadowing is not something that should be forbidden by rule (or
principally avoided).

I don't know what you mean by "local variables shadowing each other".
(Two declarations of the same variable is just an error, isn't it?)

> 
> If you want a warning on normally, but have particular reason to need an 
> exception, use pragma push/pop :

Pragmas, as I know them, were generally compiler/platform specific.
Has that changed?

(Since we developed for multiple platforms it wasn't the programmer's
"task" to place platform-specific pragmas in his code; it wouldn't
have worked elsewhere. - I seem to recall that the build-management
supported all the platform specifics.)

> [...]
> 
> This is ugly, but it stands out as clearly doing something unusual.  I 
> use it occasionally.
> 
> Or maybe you have a habit of "constifying" your loop variables.  You 
> like to write :
> 
>      for (int i = 0; i < 10; i++) {
>          const int _i = i;
>          const int i = _i;
>          ...
>      }

I don't see what the intention of such code would be. - It just looks
either wrong or at least unnecessary convoluted to me. (It appears to
me like the "constructed problems" that we regularly see from bart.)
I don't think it's sensible to argue about such, but if you think so,
maybe you can create a better example to show your point.

> 
> so that inside your loop, the variable is "const" - that would catch any 
> mistakes you make that accidentally changed the loop variable inside the 
> loop.  Clearly you don't want to write those #pragmas for every loop! 
> You can use macros:
> 
> [...]
> 
> Now you have :
> 
>      for (int i = 0; i < 10; i++) {
>          Constify(i);
>          ...
>      }

Ah! - Is it that they wanted an Algol 68 like loop-variable semantics
in "C" and introduced such [clumsy] "Constify" for that? (Amazing! :-)

(A lot of "fancy" stuff they seem to have added during past decades.)

> 
> I am not saying that I am recommending this particular practice - I am 
> merely showing how many warnings can be on in general while you also 
> have the flexibility to bypass them at times.

Thereby solving the issues that were invented in the first place. ;-)

But okay. - I see where you're coming from. And I see that what would
have been considered just unwise in former days could now in some way
be addressed. (It wouldn't serve me, but I acknowledge that it might
be a way that could help others; provided they make use if it.)

>> [...]
> 
> There are, I think, almost no circumstances in which you would want / 
> all/ warnings supported by good compilers.  "-Wall" in gcc does not mean 
> all warnings - at least, not for a very long time.  I view it more like 
> "all compilations should use -Wall".  It is intended that the warnings 
> in "-Wall" suit a wide range of uses, with few false positives in real 
> code. 

This very much sounds like what the compiler vendors back then had as
default warnings. (And what lead us to a no-default-warnings-visible
basic policy as landmark for delivering source versions.)

> Some of the "-Wextra" warnings are more divisive, though I 
> personally would like to see "-Wuninitialized" and "-Wimplicit- 
> fallthrough" in "-Wall".  I'd also like to see "-Wall" enabled by 
> default, but that's not going to happen :-(

Support of warnings has changed over the decades and I'm certainly
not competent to talk about the current state. I was merely advocating
against the "activate all warnings" philosophy; but I'm not sure any
more that this is what you actually had intended to say.

> 
> The point is, there is no downside to enabling a warning that never 
> triggers because you don't code that way. 

Yes, undoubtedly. - If you don't see it that's fine. :-)

The critical point discussed were the warnings that are displayed but
are just annoying. - And I still think this depends, as said, on the
individual, the language, and some other factors.

> And for many warnings that 
> might be triggered despite the code being correct, there is no downside 
> in writing code in a clearer manner that avoids triggering the warning 
> (like adding comments for implicit fall-throughs).

Well, that sadly cannot be generalized. (The shadowing theme, e.g., is
not a question of "clear code". YMMV.)

> [...]
> 
>> In professional contexts it's a bit different. You'd certainly annoy
>> professionals if they see many unfitting and just distracting warnings.
>> Build-settings (including warnings) are usually defined and provided,
>> and processes and QA methods and instances add another stage of safety.
>>
> 
> If your builds produce warnings during normal builds, either you have 
> written something wrong (or at least questionable) in your code [...]

Erm, no. - I think that should (at least meanwhile) have got clear.

Janis
Technical Details
Message-ID: <11a5gn2$2fkbe$2@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> <119r8u3$34nce$1@dont-email.me> <119rau4$35ugq$1@dont-email.me> <119uo2c$3kc0t$1@paganini.bofh.team> <119uqnv$d6j2$1@dont-email.me> <20261005143559.157@kylheku.com> <11a1arj$1e8ks$1@dont-email.me> <11a295r$1lqal$3@dont-email.me> <11a2e2m$ef4o$2@dont-email.me> <11a2j83$1qu9n$1@dont-email.me> <11a3ati$ef4o$3@dont-email.me> <11a4vqr$2m3b2$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: Janis Papanagnou <janis_papanagnou+ng@hotmail.com>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Wed, 7 Oct 2026 15:16:18 +0200
Organization: A noiseless patient Spider
Lines: 248
Message-ID: <11a5gn2$2fkbe$2@dont-email.me>
References: <119qf6l$2rmg3$1@dont-email.me> <119r8u3$34nce$1@dont-email.me>
 <119rau4$35ugq$1@dont-email.me> <119uo2c$3kc0t$1@paganini.bofh.team>
 <119uqnv$d6j2$1@dont-email.me> <20261005143559.157@kylheku.com>
 <11a1arj$1e8ks$1@dont-email.me> <11a295r$1lqal$3@dont-email.me>
 <11a2e2m$ef4o$2@dont-email.me> <11a2j83$1qu9n$1@dont-email.me>
 <11a3ati$ef4o$3@dont-email.me> <11a4vqr$2m3b2$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 07 Oct 2026 13:16:19 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2609518"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18GtBqj/jYvpEG8UFJcp+sq"; posting-host="d33541e634ece8542e1941a047acdc12"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:FyNB/QWRcFg/0gbGmOXg5LqZd6c= sha256:ux+BQpebM2eZ55iKudY99g27MO/OoMAaifdcTjoBfhg=
	sha1:hkGPf2+hExMSo5Z/havj5KGFQs4= sha256:6uVNd66Qf2ThRCXOnqeSbwluJfX0qr0tIqb0ymdFsPQ=
Content-Language: en-US
In-Reply-To: <11a4vqr$2m3b2$1@dont-email.me>