PDA

View Full Version : Switch vs. if..else


TomMalufe
01-08-2010, 02:46 PM
I know that this really isn't a big deal and probably wont make ANY difference to the speed of my final application (since this code will probably only be called once), but I was wondering if anyone thinks it would be better to use if..else statements instead of what I'm doing here:
package com.tenoz.utils
{
public class Seasons
{
public static const SPRING:String = "Spring";
public static const SUMMER:String = "Summer";
public static const FALL:String = "Fall";
public static const WINTER:String = "Winter";

private static var _selected:String = "Fall";
public static function get selected():String { return _selected; }

public static function select(season:String):void
{
switch(season)
{
case SPRING:
case SUMMER:
case FALL:
case WINTER:
break;
default:
season = SPRING;
}
_selected = season;
}
}
}

Also... I haven't tested this code yet and I'm not sure if I can even have private static variables. I will find out soon enough though when I can test this.

Cota
01-08-2010, 03:18 PM
I personally think a switch statement is warranted here. If I remember correctly, in AS2, if and switch statements compiled down to if's. Not sure if thats still true in AS3. However, thats the route I would have taken.

wvxvw
01-08-2010, 07:29 PM
Nope, it uses lookupswitch for switch and e / ne for if-else.
Here's a lot of matierial + dumps related to the issue:
http://www.kirupa.com/forum/showthread.php?t=340147&page=2

PS private static constants are absolutely OK. However, I'd probably emulate enum for such case - comparing objects may be faster then comparing long strings (switch uses strict equality, but strings are anyway compared by value). + you don't need to handle default case - no invalid input :)

TomMalufe
01-08-2010, 08:18 PM
I don't really understand the concept of enumeration very well... I kind of figured that I was doing something like that already with this class.

wvxvw
01-08-2010, 10:43 PM
Well, enum is needed to ensure you have a fixed number of unique values of the same type.
That's the situation where you'd typically use enum:
public class Finger
{
public static const THUMB:Finger = new Finger();
public static const INDEX:Finger = new Finger();
public static const MIDDLE:Finger = new Finger();
public static const RING:Finger = new Finger();
public static const LITTLE:Finger = new Finger();
}
Because well, there won't be any more fingers... well, who knows, but, it doesn't seem like that :)
So, now, if you'd have to choose a finger, then you'd definitely choose one of the existing fingers - this would spare you validating string or numeric input to see if the number is not negative and less then 5, or that the string which is a name of the finger is exactly the same as one of the predefined finger names.
So it is in the scope of your application: there probably won't be any more seasons, or, even if there will be, they will be few (not dynamically defined) - that's why I thought that enum would be better.

ASWC
01-08-2010, 10:45 PM
Does enum exist in AS3? :p Just kidding ...

wvxvw
01-08-2010, 10:54 PM
Does enum exist in AS3? :p Just kidding ...

:) That's why I said "emulate". No, strictly speaking it doesn't. However, you may almost emulate it. It won't work when dealing with sandboxes, and you won't be able to use enum as default value for function arguments.
However, I still think that it is a better practice then string / numeric constants. Unlike singleton, it makes you write less code, and the code will be more efficient in the end. ;)

Oh, BTW, I've just realized there are 2 more singletons in AS3 - null and undefined :)

ASWC
01-08-2010, 11:00 PM
Oh, BTW, I've just realized there are 2 more singletons in AS3 - null and undefined :)
Check:
trace(null == null)//true
trace(null === null)//true
let's go add that to the singleton thread! ;)

Ravi Bhadauria
02-20-2011, 11:29 AM
switch case is better than using if....else because switch case use minimum memory. as i read somewhere.

thanks
admec

maskedMan
02-21-2011, 02:18 PM
What's the source, and can you prove it with your own research?

Robbus
02-21-2011, 03:21 PM
I like switch because you can cascade the statements by not always adding the break line.


package
{
public class ArmParts
{
private var hasUpperArm:Boolean = false;
private var hasForeArm:Boolean = false;
private var hasHand:Boolean = false;
private var hasFingers:Boolean = false;

public function ArmParts(part:String):void
{
switch(part)
{
case "upper":
{
this.hasUpperArm = true;
}
case "fore":
{
this.hasForeArm = true;
}
case "hand":
{
this.hasHand = true;
}
case "fingers":
{
this.hasFingers = true;
break;
}
default:
// nothing doing
}
}

public function getIsFullArm():Boolean{
return this.hasUpperArm && this.hasForeArm && this.hasHand && this.hasFingers;
}
}
}



var ap1:ArmParts = new ArmParts("upper");
var ap2:ArmParts = new ArmParts("hand");
trace(ap1.getIsFullArm()); // true
trace(ap2.getIsFullArm()); // false


Cheers,
Rob