Writing Bug-Free C/D Code

Daniel Keep daniel.keep.lists at gmail.com
Mon Mar 19 22:50:40 PDT 2007



David B. Held wrote:
> Henning Hasemann wrote:
>> [...]
>> Are you really sure it is a good idea to have a typedef for each purpose?
>> So a point struct would look like this for you:
>>
>> typedef int COORD;
>> struct Point {
>>   COORD x, y;
>> }
>>
>> right?
>> [...]
> 
> Absolutely.  90% of what programmers call builtin "types" are really
> *representations*.  That is, they are classifications of bit patterns
> that have nothing to do with "types".  In fact, if it's builtin, you can
> be pretty much guaranteed it's not a type.  For instance, "int" is only
> a type when every value that an int can take on has a semantically valid
> meaning for that variable, *and* when those values can be mixed with any
> other instance of "int" in a meaningful way.  In the vast majority of
> cases, that's not true.
> 
> Of course, the reason most people don't make proper types is A) they are
> lazy, and B) it implies additional cost.  However, as programs become
> bigger and correctness becomes more difficult to prove, using proper
> types becomes more important.  It's not easy to illustrate in a 100 line
> program, but it becomes perfectly obvious in a 100,000 line codebase.
> 
> Dave

I agree with you; for instance, in my research project, I made heaps of
typedefs to ensure that I never accidentally got my wires crossed.

The *massive* problem with this, however, is that it breaks
*everything*.  You can't toString a typedefed type, you can't use
writefln with it... you've basically got to start from scratch again.
Which is a massive pain if you don't have access to the source :P  You
end up just casting them back to their base type all the time, which
kinda defeats the purpose of having them.

Eventually, I ended up using structs for all of my "typedefs" because
that way I can actually make them useful.  It would be a *god send* if
we could get type extensions.

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list