Long if statements

hi,

i have fairly long if statements like this:

if (_parent._parent.curr_chan == _parent._parent.chan_one) {
        _parent._parent.chan_one.filters = _parent._parent.filter1Array;
    }
    if (_parent._parent.curr_chan == _parent._parent.chan_two) {
        _parent._parent.chan_two.filters = _parent._parent.filter2Array;
    }
    if (_parent._parent.curr_chan == _parent._parent.chan_three) {
        _parent._parent.chan_three.filters = _parent._parent.filter3Array;
    }

is it better practice (and easy for flash to handle ) to use a switch statement and/or should i alway provide an else outcome?

cheers.
G

I usually use switch statements once it goes beyond three options. It’s really for readability.

At the end of the day, once compiled into an SWF, it is coded the same way for the Flash Player, so there really isn’t much of a difference:)

you can do two things to make this both more reusable, and increase performance.


var path:Object = _parent._parent;
var filters:Array;
switch (path.curr_chan)
{
case path.chan_one :
    filters = path.filter1Array;
    break;
case path.chan_two :
    filters = path.filter2Array;
    break;
case path.chan_three :
    filters = path.filter3Array;
    break;
}
path.churr_chan.filters = filters;


Here we are setting a path so that you don’t have to type it on every line… it’s pretty intuitive.

If/Else VS Switch

The one thing I wanted to remark on was the use of switch over if/else. Now if you look at the bytecode produced by an if/else and a switch that are doing the same thing, you’ll see that it is the exact same. With that said, switch will perform better than an if/else statement because if/else allows for values in it’s expression to change dynamically, which means that when the expression is evaluated the value may have to be retrieved from a location in memory, this process takes time. Now with switch statements the case is a predetermined value that is set at compile time… which means that the your expression is compared to a set value which does not need to be pulled out of memory.

I hope this made sense, if not please feel free to ask questions.

Take Care

_Michael

there are a couple ways to make this prettier…

one think, if you should look at is creating a variable for the curr_chan… or at very least the parent.parent. ie


var secondParent = _parent._parent;
var curChan = secondParent .curr_chan;

if (curChan == secondParent.chan_one) {
   secondParent.chan_one.filters = secondParent.filter1Array;
} 

to shoten it a bit more you have the option of just cleaning up your if statement(s) or making a swf

if (curChan  == secondParent.chan_one) {
        secondParent.chan_one.filters = secondParent.filter1Array;
    } else if (curChan  == secondParent.chan_two) {
        secondParent.chan_two.filters = secondParent.filter2Array;
    } else  if (curChan  == secondParent.chan_three) {
        secondParent.chan_three.filters = secondParent.filter3Array;
    } 

or


switch(curChan) {
case secondParent.chan_one:
   secondParent.chan_one.filters = secondParent.filter1Array;
   break;
case secondParent.chan_two:
   secondParent.chan_two.filters = secondParent.filter2Array;
   break;
case secondParent.chan_three:
   secondParent.chan_three.filters = secondParent.filter3Array;
   break;
default: 
  break;
}

i think that it might work to your benefit to just work with a modified if statement if you are only using 3 conditions

Here is a look at the actionscript vs bytecode comparison of if/else and case…

I will list both the if / else and the case, but I will only have one listing of bytecode, that is because when compiled they both share the same thing ;).



if (myVar == 5)
{
	myVar = 5;
}
else if (myVar == 4)
{
	myVar = 5 - 5;
}
else
{
	myVar = 1;
}

switch (myVar)
{
	case 5:
		myVar = 5;
		break;
	case 4:
		myVar = 5 - 5;
		break;
	default:
		myVar = 1;
		break;
}

and the bytecode


movie 'IFCase.swf' compressed // flash 8, total frames: 1, frame rate: 12 fps, 550x400 px


  frame 0
    constants 'myVar'  
    push 'myVar'
    getVariable
    push 5
    equals
    not
    branchIfTrue label1
    push 'myVar', 5
    setVariable
    branch label3
   label1:
    push 'myVar'
    getVariable
    push 4
    equals
    not
    branchIfTrue label2
    push 'myVar', 0.0
    setVariable

And please note that there is a difference between the two, and they are not always interchangable, and if used correctly one can have a performance benefit over the other.

Take Care.

_Michael

Thanks Mike that was informative.:thumb:

:wink: No problem.

On a side note, I am currently putting together a book proposal to send to O’reilly and FriendsOfEd…

Assuming that either are interested, this book’s focus is on efficiency and how to program Actionscript in a way that best fits the application you are working on. I go over a few topics that are very important for any programmer to know (especially us Actionscripters, due to Actionscripts complexes :stuck_out_tongue: )…

Hopefully this all will work out :slight_smile: I promise it will be more informative than I can be on the forums. Take Care.

_Michael

wow! great replies thanks guys! always good to learn better ways of doing things, i like clean code :slight_smile:

Michael, i would be very intrested in that book :pleased:

Well I will definitly be keeping everyone up to date with how it’s looking…

Thanks for your interest, and feedback… truly means the world.

Take Care
_Michael