Archive for February, 2009

if-statements – checking strings for being null

hello again!
Yesterday I wrote some code again and had to check if a String is null or not. The check was performed in a for-loop.
Below is some example code:

public var arrToFill:Array = [];
  1. [some more code here …]
  2. private function foo():void
  3. {
  4.   var i:uint;
  5.   for( i = 0; i < 10; ++i)
  6.   {
  7.     if(!arrToFill[i])
  8.     {
  9.       arrToFill[i] = '';
  10.     }
  11.   }
  12. }

This function was called different times and I wondered, that the if-statement returned false everytime.
For me it was theoretically correct that an Array-Element with a value of ” is not empty, because ” is an empty String for me, but it is something.
ActionScript3 doesn’t work like that, so for AS3 a String with a value of ” is equal to null or 0.

Conclusion:
If you have code like this and want to preallocate an element, just set it to 1. Then you can do a simpy

if(!arrToFill[i])

and everything would be fine.

[TIDY UP YOUR CODE] :: if – elseif – elseif – elseif – else

I thought about doing an extra section called ‘TIDY UP YOUR CODE’.
From time to time I will post some tips how you can write reader-friendly code.
So Let’ start with the first topic: if – elseif – elseif – elseif – else

Have you ever seen something like this ?

if(expressionA)
  1. {
  2.   .. do something ..
  3. }
  4. elseif(expressionB)
  5. {
  6.   .. do something else..
  7. }
  8. elseif(expressionC)
  9. {
  10.   .. do something else..
  11. }
  12. elseif(expressionD)
  13. {
  14.   .. do something else..
  15. }
  16. else
  17. {
  18.   .. do something else..
  19. }

Code like this is hard to read, don’t you agree with this?
TIDY IT UP! Use a switch instead:

switch(true)
  1. {
  2.   case expressionA:
  3.   {
  4.     .. do something ..
  5.   }; break;
  6.  
  7.   case expressionB:
  8.   {
  9.     .. do something else ..
  10.   }; break;
  11.  
  12.   case expressionC:
  13.   {
  14.     .. do something else ..
  15.   }; break;
  16.  
  17.   case expressionD:
  18.   {
  19.     .. do something else ..
  20.   }; break;
  21.  
  22.   default:
  23.   {
  24.     .. do something else ..
  25.   }; break;
  26. }

In my opinion this is great :-}

Prevent using long if-expressions

Hello out there!
Today I learned how to check a lot of statements in one expression, but without having unreadable code like that:

if(this.strChar == 'a' || this.strChar == 'b' || this.strChar == 'c')
  1. {
  2.   .. do something ..
  3. }

What you can do instead is creating an unnamed Array, push the values into it and then check if the Array contains one of those values.
Let’s see:

  1. package
  2. {
  3.   pubilc class foo
  4.   {
  5.     public function foo(strChar:String = 'default'):Boolean
  6.     {
  7.       if(['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'default'].indexOf(strChar) >= 0)
  8.       {
  9.         return true;
  10.       }
  11.       return false;
  12.     }
  13.   }
  14. }

i suppose that most of the code should be understandable to you, but let’s take a look at the if-statement:

  1. if(['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'default'].indexOf(strChar) >= 0)

You could also use the following code with the same result:

  1. var arrStringNumbers:Array = new Array();
  2. arrStringNumbers.push('one', 'two', 'three' /*and so on… */);
  3.  
  4. if(arrStringNumbers.indexOf(strChar) >= 0) // give me the index of strChar (if arrStringNumbers does not contain strChar, we get -1 as result, so the if-statement is false)