🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #17

Re: Taming The Switch-Statement

#17
From: David Brown
Date: Wed, 07 Oct 2026 08:28
257 lines
12420 bytes
On 06/10/2026 19:25, Janis Papanagnou wrote:
> On 2026-10-06 12:41, David Brown wrote:
>> On 06/10/2026 11:12, Janis Papanagnou wrote:
>>>
>>> The [language specific] problem mentioned is especially a problem for
>>> newbies (who lack awareness) or folks who regularly use different types
>>> of languages (and make typos/muscle memory "brainos"). Upon reflection
>>> or instruction especially these two groups should best enable warnings
>>> (and of course read them).
>>>
>>
>> Anyone who sometimes makes mistakes should enable warnings.  And that 
>> means /everyone/ should enable copious warnings.
> 
> Well, to me, that doesn't sound right. - Of course, "everyone makes
> mistakes", that's inherently a human "proficiency"! But the amount and
> degree of mistakes very much depends on experience (and, as mentioned,
> also depending on the used language). If "everyone" (independently of
> these factors) would see more warnings than sensible for his background
> it would not only distract him but likely he'd missed the warnings that
> would be actually useful for the given individual. - Reminds me the
> masses of [OK] buttons on WinDOS systems (or Android software, e.g.)
> you have to confirm before doing some trivial actions; and if a detail
> of the precondition changes you confirmed something you didn't intend.
> (But I seem to recall you had in some previous post also acknowledged
> the individuality of the warning settings. - I may be misremembering.)
> 

Yes, I made a point about tuning warnings to the needs of the project 
and your personal style.  As a simple example, if I am targeting a 
microcontroller with hardware support for single-precision floating 
point, but not double-precision, I will have "-Wdouble-promotion" 
enabled.  On a target with hardware doubles, I would not use that.

If you are getting lots of warnings, you are doing something wrong. 
Either you have a warning enabled that clashes with the style of the 
code, and you will likely want to disable the warning, or you have lots 
of potential issues in your code and should take a long, hard look at 
the code.

But just because you are an experienced C programmer, and "-Wformat" 
rarely triggers with your code, does not mean it is a good idea to then 
disable the warning!  Leave it on.  If your code doesn't have printf 
format errors, it costs nothing - and if you one day make a mistake, you 
get the warning.

The standard for most development, IME, is that completed programs 
should build without warnings being generated.  But that is not because 
the warnings are swept under the carpet by disabling them because the 
developer considers himself / herself an expert!

>>
>> 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;
	}


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

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.)

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.


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.

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

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
	int i;		// Intentionally shadow another i
#pragma GCC diagnostic pop

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;
		...
	}

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:

#define Shadow(...) \
     _Pragma("GCC diagnostic push") \
     _Pragma("GCC diagnostic ignored \"-Wshadow\"") \
     __VA_ARGS__ \
     _Pragma("GCC diagnostic pop")

#define Constify(x) \
     Shadow(auto const _x = x;) \
     Shadow(auto const x = _x;)


Now you have :

	for (int i = 0; i < 10; i++) {
		Constify(i);
		...
	}


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.


> 
> But I think that's easy to solve. In private, individual contexts folks
> can (and should) activate what serves best. (Maybe with full-warnings
> if a person is a completely inexperienced programmer with a language.)
> 

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.  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 :-(

The point is, there is no downside to enabling a warning that never 
triggers because you don't code that way.  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).

But you are not going to want /all/ warnings.  You are not going to want 
"-Wc90-c99-compat" when you are writing modern C code!  "-Wall", and 
possibly also "-Wextra", give you a good starting point - fine tune 
after that, enabling other options and perhaps disabling some of the 
"-Wall" or "-Wextra" ones.

> 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 and the 
warnings are doing their job, or you have a mismatch between coding 
styles and habits, and choice of warning flags.  Warning-free builds 
does not mean the code is correct - but they are an extremely cheap and 
easy-to-use way to catch many kinds of mistakes early on.  A 
professional development process that does not make use of an extensive 
array of warnings (-Wall at a bare minimum) is failing its developers, IMHO.

>> I'd guess that in my projects, there are typically 50 or more warnings 
>> enabled (-Wall -Wextra pull in a lot, with some of these then 
>> individually disabled).  The solid majority are never triggered.  But 
>> they are good to have in, because sometimes I make a typo, or have 
>> some other error that I would not have written intentionally.
> 
> Fair enough.
> 
>>
>> The warning flags are also, in a limited way, part of the 
>> documentation of the project and its coding style - there in the 
>> makefile, as part of the source code, is the rule that the code shall 
>> not have variables that are set but not used (-Wunused-but-set- 
>> variable).  That's a rule for my coding - such unused variables are 
>> confusing to readers, who should be wondering why the code has them.  
>> Many warnings are for style rather than content - but good, consistent 
>> style reduces the risk of some kinds of errors.  (Of course different 
>> people or projects will have different choices of these flags.)
> 
> That's all fine.
> 
>>
>> There is nothing to be gained by experienced developers skipping 
>> warnings because "only a newbie would make that mistake".  It's like 
>> saying only learner drivers need seatbelts.
> 
> No, that comparison isn't appropriate. (See above.)
> 
> Programmers gain that they (only!) see what is actually a problem in
> the context of their staff (or individual), their language, and other 
> project related factors that affect the software production.
> 
> IME, too many warnings is as bad as a suppress all warnings setting.
> 

If you can't run your builds with "-Werrors", you are doing things 
wrong.  Your code is not ready to check-in until is is warning-free. 
But you don't achieve that by hiding the issues - you do it primarily by 
solving the issues, writing better or clearer code.  If a warning 
conflicts with common practice in your style of code, then you disable 
that particular warning - you don't disable others.

>>>>
>>>> "There are two sorts of programming languages - the ones people 
>>>> complain about, and the ones no one uses."
>>>
>>> :-)
>>
>> To be clear - that's a quotation from Bjarne Stroustup of C++ fame.
> 
> Oh, it appeared familiar to me (but I've probably forgot the source);
> it's still a very nice quote. (And very fitting given some discussions
> here.)
> 
> Janis
> 
Technical Details
Message-ID: <11a4vqr$2m3b2$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> <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>

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, 7 Oct 2026 10:28:11 +0200
Organization: A noiseless patient Spider
Lines: 257
Message-ID: <11a4vqr$2m3b2$1@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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 07 Oct 2026 08:28:12 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2821474"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/MnD+bOv8nQbIM0FMHGds+QmCZaW9cPJE="; posting-host="fd0a1bf01e024f2af36d0493face3db6"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Imslxt7llJVacjmO+3oOY6imJ/Q= sha256:82LSdZBAwufvhybnxCAVUJj1skGkhJrHcPuH3eqkiPg=
	sha1:7kHD3R5Gk84GSf+xOWE/GAsUNbU= sha256:u6nRpBiDu3pa4tHPlmbeWKtOelBBiYHehJitcstR5X8=
In-Reply-To: <11a3ati$ef4o$3@dont-email.me>
Content-Language: en-GB, nb-NO