Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 01-08-2010, 02:46 PM   #1
TomMalufe
Salmon Eater
 
TomMalufe's Avatar
 
Join Date: Jun 2007
Location: Doylestown, PA, USA.
Posts: 740
Default Switch vs. if..else

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:
ActionScript Code:
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.
__________________
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." -- Chinese Proverb.
TomMalufe is offline   Reply With Quote
Old 01-08-2010, 03:18 PM   #2
Cota
Super Moderator
 
Cota's Avatar
 
Join Date: Jul 2001
Location: NYC
Posts: 10,175
Default

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.
__________________
Color Wars™ | (kul′ər wôrs) n. - Open conflict between factions. www.theColorWars.com

cota - www.chadworkman.com | http://shavedplatypus.com
occupation: designer | flash developer | server-side developer (php, asp, mssql, mysql)
Cota is offline   Reply With Quote
Old 01-08-2010, 07:29 PM   #3
wvxvw
Holosuit User
 
wvxvw's Avatar
 
Join Date: Oct 2006
Location: Tel Aviv
Posts: 4,301
Send a message via ICQ to wvxvw
Default

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/showthre...=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
__________________
The .NET open source editor for Flash and web developers
*This would be my contribution to the project*
couchsurfing if you need it

Last edited by wvxvw; 01-08-2010 at 07:37 PM.
wvxvw is offline   Reply With Quote
Old 01-08-2010, 08:18 PM   #4
TomMalufe
Salmon Eater
 
TomMalufe's Avatar
 
Join Date: Jun 2007
Location: Doylestown, PA, USA.
Posts: 740
Default

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.
__________________
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." -- Chinese Proverb.
TomMalufe is offline   Reply With Quote
Old 01-08-2010, 10:43 PM   #5
wvxvw
Holosuit User
 
wvxvw's Avatar
 
Join Date: Oct 2006
Location: Tel Aviv
Posts: 4,301
Send a message via ICQ to wvxvw
Default

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:
ActionScript Code:
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.
__________________
The .NET open source editor for Flash and web developers
*This would be my contribution to the project*
couchsurfing if you need it

Last edited by wvxvw; 01-08-2010 at 10:48 PM.
wvxvw is offline   Reply With Quote
Old 01-08-2010, 10:45 PM   #6
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

Does enum exist in AS3? Just kidding ...
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 02-21-2011, 03:21 PM   #7
Robbus
Senior Member
 
Join Date: Oct 2008
Location: UK
Posts: 167
Default

I like switch because you can cascade the statements by not always adding the break line.

ActionScript Code:
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;         }     } }

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

Cheers,
Rob
Robbus is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:31 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.