[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: casts considered harmful



> Why on earth do we specify a constant pointer to a pointer to a
> void for this?  All it does is force people to use casts, which
> are just a way to mask programmers' mistakes so that they don't
> notice them until they get a hard-to-track-down bug report.

Michael, I think I'd disagree with this.

If casts are ever used to mask programmers' mistkaes, that is a flagrant
mis-use.  Casts should be used, as should every other tool at a software
engineer's or programmer's command, to document the code to (a) make it
more reliable [have the computer understand you better] and (b) document
it [have other people understand you better].  People who just stick in
casts to shut the compiler up are rote programmers who don't really know
what they're doing.  You shouldn't trust their code anyway.

In general, void pointers are in the language to provide for a mechanism
to allow a "general pointer" to be passed, which at compile time, of
course, must be a real pointer.  A real pointer will never be a void
pointer, and so we use casts.  Why is this good?  Any code that's
declared with void pointers allows you too much latitude for comfort.
Therefore, the compiler SHOULD complain about the type mis-match; and
the competent software engineer should then THINK and make a CONSCIOUS
decision that he or she is doing the Right Thing (tm) with the type,
before inserting a cast there.  You should also make sure to document
the type that the object is supposed to be.

Having said that ... the above was just a knee-jerk reaction to your
paragraph, and I'd have to look at what you were talking about before
deciding whether it was warranted there.  But someone obviously thought
it was.

Another good use is casting constants!  If I'm supposed to be working
with character pointers, and I say:

#define NULL	(0)
	if (NULL == cp) ...

I get no errors if, perchance, I should have used chp because cp is a
pointer to a crackerbarrel object.  However, if I type cast:

	if ((char *) NULL == cp) ...

then the compiler will catch my error at compile time, instead of run
time, which is the RIGHT way to do it.

Eh?

;-)

IMHO only, of course.

--
/*********************************************************************\
**
** Joe Yao				jsdy@tux.org - Joseph S. D. Yao
**
\*********************************************************************/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index] []