bug me now / bug me later

Jim Harkins jharkins at sagpd1.UUCP
Fri Jun 8 09:26:24 AEST 1990


Lets say you have the following (you ADA people, please bear with me):

	#define SIZE	6
	#define STEP	2

Which is better:

	a.	for(i = SIZE; i != 0; i -= STEP)
or
	b.	for(i = SIZE; i > 0; i -= STEP)

Where this makes a difference is suppose SIZE is changed to 7.  Obviously
'a' goes into an infinite loop, while 'b' stops.  In the real world SIZE and
STEP could be variables that have been input by a user and manipulated a
gazillion times before being used in the for loop.

With method 'a' a bug is easy to spot, thus easy to fix.  But the bug may not
appear at a good time, the user may prefer that his system limp along rather
than hang completely.

With method 'b' the bug is harder to spot.  If you test your code well you
should catch these things, but a man named Murphy became famous anyway.  I
believe method 'b' is preferred by 'defensive programmers'.  It is also
possible that with method 'b' the loop body stomps over somebody elses
memory, with unknown but probably bad results.


So, how do you all feel about this?  As I'm writing for a military contract
I've cross-posted this to comp.lang.ada, I hope they don't get too upset.  And
please hurry, this came up for a reason :-)


-- 
jim		jharkins at sagpd1

I hate to see you go, but I love to see you walk away.



More information about the Comp.lang.c mailing list