Coding Quirks
After over a month on the job, I found a bunch of quirks and inefficiencies when I was told to optimize a collection of programming codes. In some case(s), it was totally unacceptable, according to a computing friend of mine. Well, I'll attempt to put those coding practices currently employed as simply across as possible (since they are probably no different from egyptian hieroglyphs for most people).
1. For the love of assignments...
Quirky code:
int mynumber = 0;
mynumber = 5;
What does it mean:
I need a container to store a number.
Let's call it mynumber.
Let mynumber be an integer (i.e. whole number)
Let mynumber contain the number 0 initially.
Now, set mynumber to contain the value 5.
Layman terms/equivalent:
I want a basket with a random number of apples.
Now, take out all the apples.
Now, I want to put in 5 apples. (Does it sound geh gau? Hehe...)
Quirk:
Why do you need set nynumber to 0 when you could initially set it to 5?
Optimized code:
int mynumber = 5;
2. One can never be too specific...
Quirky code:
if (condition1==true AND condition2==true AND condition3==true) Then
//Do something
else if (condition1==false OR condition2==false OR condition3==false) Then
//Do something else
End
What does it mean:
If condition1, condition2 and condition3 is true, then [do something]
If not, then, if condition1 is false or condition2 is false, or condition3 is false, then [do something else]
Layman terms/equivalent:
If the shirt is green colour, [do something]
Otherwise, if it is red, or if it is blue, or if it is yellow, or if it is white, or if it is black, or if it is pink, or if it is dark green... etc. etc. etc. till the cow finds its way home, gets old, goes cranky and hallucinates about what other colours it might possibly see and dies... then [do something else]
Quirk:
Well, there is something with an 'otherwise' equivalent which is not used...
Optimized code:
if (condition1==true AND condition2==true AND condition3==true) Then
//Do something
Else
//Do something else
End
3. Double negatives...
Quirky code:
if (condition1!=false) Then
//Do something
End
What does it mean:
If condition1 is not equal to false, [do something]
Layman terms/equivalent:
Imagine someone who talks in this manner: "If you are not (not (not (not tired))), would you like to... etc."
Optimized code:
if (condition1==true) Then
//Do something
End
4. To be absolutly absolute...
Quirky code:
x = abs(-5 + 8 / 200)
if ( abs(x) > 5 ) Then
//Do something
End
What does it mean:
Set x to the value of (-5 + 8 / 200) and 'absolute' it (i.e. make it positive)
if the absolute of x (which is already 'absoluted') is greater than 5, [do something]
Layman terms/equivalent:
Log into your email account.
Then, log into your email account (via another web browser window) to check for mail.
Optimized code:
x = abs(-5 + 8 / 200)
if ( x > 5 ) Then
//Do something
End
Well, there are many more quirks that I came across, but I guess I shall stop for now lest my blog becomes a groggy-inducing aid. =)