Java: Helping the world build bigger idiots

Jerrold Leichter leichter at smarts.com
Wed Sep 21 12:27:53 EDT 2005


| > One thing to consider is that an idiom like this solves an annoying problem.  
| > Consider a linear search through an array:
| > 
| > 	for (i = 0; i < lim; i++)
| > 	{	if (a[i] == target)
| > 		{	<do something>
| > 			break;
| > 		}
| > 	}
| > 	/*
| > 	 * Did we get here because we matched or because we
| > 	 * failed to match?
| > 	 */
| 
| No, we got here because we didn't know basic C usage.  Come on
| people, please stop creating these fake illustrations.
Oh, come on.  This loop has a trivial increment and a trivial stopping 
condition, for the purpose of illustration.  But even here, it's not so 
simple.  Better style in general is to limit the loop variable's scope to the 
loop:

	for (int i = 0; i < lim; i++)
	{	}

Now i isn't accessible after the loop is done.  My personal style has always 
been to treat the loop variable as local to the loop, even when C didn't let
you declare it that way.  There are exceptions - especially cases where you 
traverse the same array in two consecutive loops, the second picking up where 
the first ended - but I've generally found that to be a better style.

| A real C programmer would have known that, if i == lim, there
| was no match.  This is so trivial it beggars belief that it
| needs to be pointed out in a forum like this.
| 
| > Personally, I sometimes use:
| > 
| > 	for (i = 0; i < lim; i++)
| > 	{	if (a[i] == target)
| > 			goto found;
| > 	}
| > 
| > This draws shock and horror from some code readers, but I don't care.  :-)
| > Note how much it looks like the exception-based code.
| 
| It only draws gasps from people who don't know C.  The goto that
| is famously considered harmful is not spelled "goto" in C, but
| rather "longjmp"; it's not used all that often and does need
| careful handling.  The C goto statement is purely a local goto
| and scares nobody who has grown up.
Nice to know we're all adults here :-)

							-- Jerry


---------------------------------------------------------------------
The Cryptography Mailing List
Unsubscribe by sending "unsubscribe cryptography" to majordomo at metzdowd.com



More information about the cryptography mailing list