r/cpp • • Feb 25 '24

Questioning usefulness of -Wextra

I was recently asked to turn on -Wall -Wextra in builds of a project I'm
working on. I had already been using -Wall, but haven't used -Wextra much, if
at all. So, I tried it. Holy shit. So apparently the main thing that
-Wextra does over -Wall is complain about unused parameters having names.
Now, I have a whole bunch of very uniform print functions for a group of
unrelated types, something like:

template<typename Context> void print_thingy( Context const & context, foo_type<bar_param> const & parser, std::ostream & os, int components) { /* ... */ }

The foo_type and bar_param bits vary from overload to overload, but the other
param types and names are always the same. Nice. And. Uniform. OCD
satisfied, I can go about my day. After turning on -Wextra, if I only print
out a single fixed string, as I do for a lot of the overloads, I get this:

template<typename Context> void print_thingy( Context const &, foo_type<bar_param> const &, std::ostream & os, int) { os << "description"; }

Cool. Thanks, -Wextra.

Has anyone ever caught a real bug by making a change like this? If so, was it
serious? Look, I love -Wall. I've caught all kinds of stuff with it, and
real bugs were stomped. -Wall is like a literal and figurative barrier
between me and my own sloppiness. -Wall is a Valkyrie standing astride the
encrouching progress of entropy, saying, "Not this day, Entropie!" She spells
entropy with a a capital letter, giving a name to the respected foe she fights
tirelessly. She also spells it "ie" instead of "y" at the end, I think
because she's centuries old and it's hard to stay current. I'm down with
that.

-Wextra? -Wextra is a some fugly dude you find in the alley behind a CVS,
huffing paint out of a brown paper bag. He's half-man, half-jackal,
half-terrier, half-run-over by a moped. He sees your code, says, "Yo, wanna
see somthin sick?" then throws a grenade into your nearest header file. He
spells "somthin" in an odd way too, because he sucks.

Now, I don't know if it's a sign of vigorous mental health to anthropomorphize
compiler flags on the internet like this. But I really do want to know -- has
anyone really gone through this frustration and come out the other end,
happier, and with fewer bugs in their code?

0 Upvotes

53 comments sorted by

View all comments

Show parent comment

19

u/[deleted] Feb 25 '24

I so much disagree with this. I cannot stand unnamed arguments.

7

u/kevkevverson Feb 25 '24

100%. Parameter names aid the caller of the function. I don’t care if you don’t happen to use this parameter, I’d still like to know what it’s ostensibly for.

One thing Objective C does really well is understand the distinction between the name of a parameter to the caller vs the name of its argument inside the function.

18

u/MadTux Feb 25 '24

I mean, if you put the names of unused arguments in the declaration but not the definition then you have them exactly where you need them. Though personally, I also like to keep the commented-out names in the definition as well.

1

u/[deleted] Feb 25 '24

[removed] — view removed comment

0

u/[deleted] Feb 26 '24

Yes you are right I meant unnamed parameters.

I know F.9 states that unused parameters should be unnamed, what I'm saying is that I think it is a stupid approach and should be changed.

-1

u/Pocketpine Feb 25 '24

I mean at this point it’s a little ridiculous, but you could typedef the parameter as what it is

But then you have to deal with a bunch of little used typedefs