David said:
And it does. If a programmer codes a cast, he intends a conversion. If
a programmer requests a conversion, he is vouching for its safety.
This is as true as if a programmer writes "i++;" he is vouching that
it is safe to add one to 'i'. This is axiomatic stuff. There is no
possible counter-argument.
So, a person who writes a cast to shut up a warning, without
understanding that the resulting conversion will have consequences he
wasn't aware of, can be said to have intended that those consequences occur?
A person who would vehemently deny possession of sufficient
understanding to decide whether the code is safe, who would tell you he
put the cast in only because the compiler's diagnostics suggested
putting it in, can still be said to have vouched for the safety of the code?
You use the verbs "intend" and "vouch" in a manner quite inconsistent
with my understanding of their meaning. I can't quite see the usefulness
of defining those terms in any way consistent with the way that you use
them. Having defined them in that way, they no longer support the
conclusions you use them for.
Simply put, it reduces the possible cases. There is, without the cast,
one possible case that is not the case -- that the programmer had no
idea there was a conversion and therefore never considered its safety.
The cast rules out this case.
Every one of the cases I was referring to involved a cast written by a
programmer who had no idea that there was a conversion (or at least,
that it was a conversion with non-trivial consequences), and who
therefore had not in fact considered whether or not it was safe.
They are not counter-examples. If a programmer writes '(unsigned) j'
he is vouching for the safety of the cast, just as when he writes any
code he is vouching for it. Just because he was mistaken in doing so
doesn't change the fact that he did so.
If someone signs a document he does not understand, and it contains
assertions that he would have disagreed with, had he understood them,
does this mean that he now agrees with those assertions? Writing his
signature on the document does not magically change his mind to match
the words of that document.
Similarly, inserting a cast in code does not magically change the
developers intention to match what will actually happen. A convention of
inserting casts only when you are willing to vouch for their safety does
not magically bring people into conformance with that convention who
wrote their code without being aware of it, or without a desire to
conform to it. I do not defend the competence of these programmers; I
merely point out that they do exist, and code written by such people has
to be dealt with occasionally.
That's strange, because there really should not be that many
conversions.
In the typical data path for my programs, data is read from a file into
objects with typedefs associated with the HDF library like float32. It
is then passed to the SDP Toolkit library which requires that they have
the type PGSt_double (it is not essential that you know what those
libraries are; it is sufficient that you understand that they are third
party libraries that define platform-dependent typedefs for the types
used in the library's interfaces). For some purposes, the data is passed
to C standard library functions which require that it be of type
'double'. When all processing is complete, the data is written to the
output file with the type float32 again.
That's a minimum of three casts for every piece of data flowing through
the program. One of the first things I did when I became responsible for
these programs was to rearrange it so that the minimum was actually
achieved; the original code performed at least twice the minimum number
of conversions. It's not uncommon for half the lines that refer to a
give piece of data to involve converting it from one type to another.
That's not true in the science code, but actual science code constitutes
only about 10% of my programs; the remaining 90% is involved with
pushing data around from one place to another.
... I agree that prohibiting all implicit conversions is not
a good job of striking the right balance. Almost any "never" or
"always" rule will cause significantly sub-standard code in at least
some cases.
It's not enough to have a coding rule. You have to understand the
reason for the rule and know when following the rule doesn't vindicate
the reason for the rule. In those cases, you should break the rule.
That would violate our contract with our client. It's not an option.