Friday, February 27, 2004
WARNING! Technical Rant Below.
While talking to Al, he mentioned Engineers being generally more cautious and those with a CS background being more creative. This reminded me of a debate I had with another guy at work (strangely enough, his name is Alex). The other Alex is an Electrical Engineer and he had a pretty interesting view on FOR statements which us CS grads a different view. To demonstrate, take the following code snippet:
for (i=0, j=last; i < j; i++, j--)
{
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
I actually like this bit of code (and I think i actually used it somewhere). It's probably not the most elegant, but no one's ever accused me of being a programming genius. Alex argues the above code is just plain wrong. Although I point out that it works (and is absolutely technically correct), having a "variable" exit condition in a for loop is just plain wrong. "That is not what a FOR loop is meant to do!" he exclaims. If you want to do something like that, you HAVE to use a WHILE so he says. Oh, and he didn't realize that it was possible to have 2 variables in the FOR statement, but considering his other views are you surprised? To compare, this is the same snippet as a while loop:
i=0;
j=last;
while (i < j)
{
temp = string[i];
string[i] = string[j];
string[j] = temp;
i++;
j--;
}
How this is more readable is beyond me. It's longer, the conditions of the loop are scattered all over the place, and it does exact the same thing. Now if you were doing something most complicated (pretty much a given) and this loop extended beyond the physical boundaries of your screen. . . . it's really going to take you longer to figure out what it's doing because you have to take in exactly what every line is doing.
The other point came from the use of this:
for (i=0; i < last; i++)
{
if (string[i] == token)
{
found = true;
break;
}
}
What's the problem here? Well, according to Engineer Alex, it's the break statement in the middle of the FOR loop. That's a major no-no. According to him, a FOR loop MUST execute a fixed number of times. It cannot be variable, have an additional exit condition, look at you funny. . . . He didn't even like it when I suggested that you could do this:
for (i=0; ((i
if (string[i] == token) found = true;
}
Apparently, any of these task should only be done using a WHILE loop according to the book of Engineer Alex. When can you use a FOR loop? Well, using the guidelines he specified and considering real-world applications. . . NEVER. I just feel so avant garde now! I'm such a coding rebel! :-p