Coding Quirks 2
And since I'm on medical leave today, I might as well rant about the kind of quirky stuff that I saw that is blood-vomitting-inducing, heart-wrenching and out of this world. Okie, I exaggerate, but seriously, the coding is bad enough.
In the midst of all the debugging that I had to do (well, for example, I found 11 bugs on just a simple window to create a list of items and to generate a list of user-defined selections of that same list), here are some of the quirks that I came across...
1. Find me if you can...
Quirky code:
void main() {
int mynumber = getnumber(5);
}
int getnumber(int number) {
getnumber2(number);
};
int getnumber2(int number) {
getnumber3(number);
};
int getnumber3(int number) {
return 5;
};
What does it mean:
In the program, I want to store a number. Let's call it mynumber. Now, assign that value to be getnumber, which will return getnumber2, which will return getnumber3 which will return 5.
Layman terms/equivalent:
A, B, C and D are in the room facing each other, and A wants to know the name of D. As such, A asks B (in an obvious manner such that D would know it) to ask C to ask D for D's name. After that, D has to tell C to tell B to tell A the name.
Quirk:
Why can't you simply just assign the value of 5 to mynumber???!!! (i.e. simply ask D his name!)
Optimized code:
int mynumber = 5;
2. Cut-and-paste frenzy...
Quirky code:
for (int i=0; i!=5; i++) {
if (i==0) print (0);
else if (i==1) print (1);
else if (i==2) print (2);
else if (i==3) print (3);
else if (i==4) print (4);
}
What does it mean:
I want to print out 0 to 4 on the screen. So, let's start with a number. Let's call the number 'i'. Let 'i' start from 0, and increase it by one each time. Once 'i' reaches 5, stop the increments. Now, as 'i' increments, check its value. If it is a 0, print out a 0. If it is a 1, print out a 1. If it is a 2, print out a 2. If it is a 3, print out a 3. If it is a 4, print out a 4.
Layman terms/equivalent:
In order to type out the numbers 0, 1, 2, 3, 4 on a word processor (i.e. Microsoft Word), first you get someone to count from 0 to 4 slowly. As that person is counting, you get someone else to confirm the number that first person said. Upon confirmation, you type out the number. What the @#$^%???!!!
Optimized code:
The terrible, but more 'forgiving' method in view of the above ultimate example:
print(0);
print(1);
print(2);
print(3);
print(4);
Preferred method:
for (int i=0; i!=5; i++) {
print(i);
}
3. Processing frenzy...
//To duplicate a list of items...
for (each item in Listbox1) {
int position = getindex(item);
//Add the item if the index is valid
if (position != -1) ListBox2.add(item.Text, position);
}
private int getindex(listitem item) {
for (int i=0; i