Unrolling Code Closures for Undergraduates
Thursday, March 29th, 2007One consistent tendency I’ve noticed among undergraduate programmers is a persistent and incorrect belief that the number of lines of code is somehow tied to code efficiency. The thinking goes that the fewer lines they have, the faster the program will run. (Why they even care about speed at all is a story for another day.) For example, they get peeved when I write this:
int x;
int y;
int z;
instead of this:
int x, y, z;
The brighter ones may not be bothered by that, but this gets them all in a tizzy, nonetheless:
int low = 1;
int high = 1;
for (int i = 0; i < 50; i++) {
System.out.print(low);
int temp = high;
high = high + low;
low = temp;
}
They want to see this
int low = 1;
int high = 1;
int temp;
for (int i = 0; i < 50; i++) {
System.out.print(low);
temp = high;
high = high + low;
low = temp;
}
They love taking code out of a loop, even when the code in question (a declaration) doesn’t actually do anything; and they certainly don’t care if their resulting code is less readable. In fact, they sort of take it as a mark of honor if their code looks complex. They’re going to love closures.
(more…)