This week, I refactored parts of a group programming project that had been completed last year. Browsing the source code, looking for opportunities to polish and slightly enhance the program, I had several moments of „what the f… is this?“ There is even a web site dedicated to WTF moments.
I will share an example here. It looks like a loop, but is a quite complicated way of performing an assignment just twice:
for(i=1; i<3; i++) {
if(i==1) {
FB = 4*CNo;
} else if(i==2) {
FB = 4*CNo+(SPF*BPS);
} Buffer[FB]= 247; Buffer[FB+1]= 255; Buffer[FB+2]= 255; Buffer[FB+3]= 15;
}
I reduced the length of the code without changing functionality:
FB = 4*CNo; Buffer[FB]= 247; Buffer[FB+1]= 255; Buffer[FB+2]= 255; Buffer[FB+3]= 15; FB = 4*CNo+(SPF*BPS); Buffer[FB]= 247; Buffer[FB+1]= 255; Buffer[FB+2]= 255; Buffer[FB+3]= 15;
There were more WTF moments and the code ruined a whole afternoon in the first week of the new year.
Lesson learned: education in programming is important and our fine offerings obviously do not reach all students in time.