🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

4 total messages Started by David Brown Wed, 07 Oct 2026 08:28
Re: Taming The Switch-Statement
#17
Author: 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
> 
Re: Taming The Switch-Statement
#29
Author: 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
Re: Taming The Switch-Statement
#32
Author: David Brown
Date: Wed, 07 Oct 2026 14:40
416 lines
19503 bytes
On 07/10/2026 15:16, Janis Papanagnou wrote:
> 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 can't reconcile "getting lots of warnings" with "builds must be 
warning-free".  If your builds are warning-free (once you have fixed 
whatever issues you had in the code, or possibly modified your choice of 
warning options), you are not getting lots of warnings.  You are not 
getting /any/ warnings, except when you make a new mistake in your code 
or write something new that is very unusual.

Since the warning options you have are not then giving any false 
positives, or hindering you from writing the code you want, I cannot see 
why you then would want to disable them.  It gains you nothing.

And there is no "confirmation fatigue" when there are no warning 
messages to get lost in.

You make it sound like I am recommending getting lots of warning 
messages from your compiler rather than lots of warning options!

(I think there has been some level of miscommunication in this 
discussion, which I hope is at least partially cleared up by the end of 
this post.)


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

No one has suggested that you use a "full-warnings" setting - quite the 
opposite.  I have recommending having /lots/ of warnings, but most 
certainly not /all/ - tune the choices to your needs (and the coding 
standards required by the project, if applicable).

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

You said that "newbies" should be enabling more warnings than "more 
experienced people".  I don't know (and I don't think it matters) 
whether that distinction is from self-evaluation or anything else.

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

Yes - and gcc warned me about that when I tried the code.  I have made 
such typos in real code, and if the compiler had not immediately told 
me, I am sure it would have led to a lot of confusion about why my code 
did not work as expected.

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

I don't think a default branch should be mandated - but different people 
have different opinions there.  Since both these rules can be checked 
automatically by gcc (-Wimplicit-fallthrough, -Wswitch-default), it 
would be almost absurd not to have these warning options enabled.

(I have been referring to gcc options throughout this thread, since that 
is the compiler I use most myself.  Other compilers are likely to have 
somewhat similar options, though of course details can vary.)

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

"__attribute__((fallthrough))" has been around for quite some time - but 
perhaps not since the early 1990's !

> 
>>
>> 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?

Yes.

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

It handles a wide range of likely patterns in comments (the manual page 
gives the details for those that like reading regular expressions).

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

To be fair here, I have been giving information about modern gcc.  The 
compiler gains new warnings with every version.  The 
"-Wimplicit-fallthrough" option is from gcc 7 in 2017, while 
"-Wswitch-default" is from gcc 3.3 in 2003.

I don't change compiler versions for existing projects without very good 
reason, but when starting projects I usually choose the newest version 
with an appropriate level of support for the target, and I update my 
list of warnings (and other flags) in my makefiles to suit.

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

int a;

void foo(void) {
	int a;		// Shadows global a
	for (int a = 0; a < 10; a++) {	// Shadows local a


Shadowing globals is more likely to happen by accident.  Most people 
don't want a complaint about shadowing for their local variables "y0" 
and "y1" to suddenly appear when they have #include <math.h> and choose 
std=gcc17 instead of std=c17.  (These are Bessel functions provided by 
several maths libraries as extensions.)

But it is not uncommon to disallow local variables from shadowing each 
other.  One opinion here is as good as any other - it's a style choice.

> 
>>
>> 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?

No.  Warnings are also compiler-specific.  (Warnings and pragmas are 
mostly target-independent, however.)  Some projects are naturally 
compiler-specific, others aim to be as compiler agnostic as possible, 
and some use conditional compilation to adapt.  You know all of this - I 
am sharing examples from gcc, and you and anyone else can adapt to suit 
the tools you use.

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

I don't intend this as a recommended practice in code.  But people write 
weirder stuff - you occasionally come across someone using a macro like 
"FOR_LOOP(i, 10)" because they prefer it to normal "for" loops.  And I 
believe it is common to see modification of a loop variable inside the 
loop is a very bad idea - if the loop variable were "const" inside the 
loop, any accidental modification would be caught as an error.  I don't 
imagine many people would bother to use an extra const variable to get 
that effect.

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

Do you mean the type inference with "auto" ?  It's new to C23, but been 
in C++ since C++11, and in gcc as "__auto_type" for a very long time.

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

Maybe there is a particular pattern you use regularly where you have 
local variables that shadow each other, and we could use that?  Since I 
can't think off-hand of a case where local variable shadowing would help 
the clarity of real code, it's difficult to think of a realistic example!

When I look in my own code for cases where I have used diagnostic push 
and pops, the most common cases is for the "-Wpadded" warning when a 
structure has had added padding for alignment.  Padding happens 
naturally in C code, and is usually not a problem - the warning would 
trigger on a lot of existing code and third-party code.  But sometimes 
you want to be sure there is no padding, such as when a struct 
represents a network packet or other external structure.  So here I 
would turn on the warning around the network struct definitions, then 
restore the original state of the warning flag.

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

Different compilers have had different policies for default sets of 
warnings.  gcc is quite lax by default (though it has improved with 
time).  I've seen compilers that are downright idiotic in their default 
choice of warnings - IMHO, of course.

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

There seems to be some miscommunication here - I have never suggested a 
"activate all warnings" philosophy.  I have suggested activating 
warnings for lots of things, as long as they don't clash with the coding 
style of the project or programmer - and I advocate against the idea 
that "experts" should disable warnings that would mostly only be 
triggered by "newbies".

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

Of course.

In my projects, I will often have lots of code provided by third parties 
(manufacturer's SDKs, RTOS's, network stacks, etc.).  Sometimes the 
quality of that code is questionable at best, and often it is written in 
a noticeably different style from my own.  So I use different flags for 
that code compared to mine (sometimes going as far as to use "-fwrapv 
-fno-strict-aliasing" on the third-party code), with more warning flags 
on my own code.

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

Let me re-phrase a little.  If writing the code in a clearer manner 
means a particular warning is not triggered, then it is better to write 
the code clearly and have the warning enabled, than to write unclear 
code and disable the warning.  Thus it is better to avoid implicit 
fall-through warnings by adding a comment in the code instead of 
disabling the -Wimplicit-fallthrough warning.

Other things are more a style choice where people will have differing 
opinions on what is "clear" in the code - and there will be less 
consensus about whether or not a warning option should be used.  That's 
fine too.

>> [...]
>>
>>> 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
> 
Re: Taming The Switch-Statement
#62
Author: Tim Rentsch
Date: Thu, 08 Oct 2026 14:24
16 lines
1956 bytes
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:

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

It is true.  And the fix is usually to omit or disable more -W
options. ;)
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads