[LCP]Rounding questions

Greg Black gjb at gbch.net
Sat Sep 7 18:01:05 UTC 2002


Robert Gehring wrote:

| IMHO: Casting makes a lot of sense at least when you try to make your code 
| reliable trough checking it with lint. lint likes casting very much, as you 
| can easily try out. And there is one more point: The day may come when 
| someone other has to look at your code ... Casting is one way of documenting 
| your code for other people. It explicitly says what you meant to do.

This is all wrong.  It's not just your opinion, it's wrong.
Properly written C89 code almost never needs casts and when they
are used they shout out that the code was written by somebody
who just doesn't understand C.

If you use a version of lint that likes unnecessary casts, throw
it away because it's telling you nonsense.

If somebody else is looking at my code, I don't want them
running around in circles wondering why I put some cast into
code that shouldn't need it.  I want them to read it and
immediately understand it.

Finally, when you develop a habit of using unnecessary casts,
then you are almost certain to include casts that are actually
wrong -- but your cast prevents the compiler from issuing you
with a useful diagnostic and then you (and the poor unfortunate
people who have to clean up after you) have to take time to find
out something that the compiler could have found for free.

Just don't use casts -- except in the very few cases where they
are required.  There are extra points for providing a single
example of such a case.

Greg




More information about the linuxCprogramming mailing list