[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)

Welcome

Hello and welcome to my new website.
It’s the, ehm… let me think… third or perhaps fourth try of creating an own site.
Of course I’ve created some websites before but hadn’t have the time to do my very own site.
When I think deeper, the biggest problem was the content. What are people interested in? Should I post a simple Blog about my life, hobbies, car, etc.? No damn – nobody wants that crap!
I once had a site with some 3D-Graphics, but when I look at these today, I only think “What noob did this?”.
So after having a domain for about one and a half year, with nearly no content on it, I got the idea: ActionScript 3.0 would be the main topic of my website. Additional to this there may be a little bit PHP-Coding, C++(still learning the language at the moment) and perhaps some design-related content.