Help a C++ coder see the light

Dave Cole djc at object-craft.com.au
Thu Jan 30 20:33:19 EST 2003


>>>>> "Brandon" == Brandon Van Every <vanevery at 3DProgrammer.com> writes:

Brandon> Dave Brueck wrote:
>> On Thu, 30 Jan 2003, Mark Charsley wrote:
>>> I'm used to the compiler/linker giving me a lot of reassurance
>>> that my code works.
>>  That's common, but in many cases it's a _false_ sense of
>> reassurance.

Brandon> It kills many problems before they arise.

>> Even in C++ I end up checking all uses of a refactored function
>> despite what the compiler says because it can catch only the
>> simplest of problems.

Brandon> Sounds like your functions must do an awful lot if they need
Brandon> so much checking.  Maybe your functions should be smaller,
Brandon> more incremental, easier to verify?

Ahhh yes, the excellent safety net of the compiler/linker.  It
compiles, my work is done...

- - calculate-wage.c - - - - - - - - - - - - - - - - - - - - -
#include <stdio.h>

float calculate_weekly_wage(float hours, float rate)
{
    if (hours > 37.5)
	return 37.5 * rate + (37.5 - hours) * rate * 1.5;
    return hours * rate;
}

int main(int argc, char *argv[])
{
    float hours = 45.0;
    float rate = 10.0;

    printf("Worked %.2f hours at $%.2f hourly rate.\n", hours, rate);
    printf(" -> wage (including overtime) is $%.2f.\n",
	   calculate_weekly_wage(hours, rate));
    return 0;
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

% cc calculate-wage.c -o calculate-wage   
% ./calculate-wage 
Worked 45.00 hours at $10.00 hourly rate.
 -> wage (including overtime) is $262.50.

What!!!  How can that be!!!  It passed through the compiler/linker
safety net.

Ahh...  This gcc is crap.  Maybe I need to use the intel compiler.

- Dave

-- 
http://www.object-craft.com.au




More information about the Python-list mailing list