Suggestion: Switch Statements

Started by Galen, Fri 23/01/2009 20:09:42

Previous topic - Next topic

Galen

Quite simply, my suggestion is too implement switch statements.

So instead of:
Code: ags

if(i==1)
{
 DoSomething();
}
else
{
 if(i==2)
 {
  DoSomethingElse();
 }
 else
 {
  DoUnmanaged();
 }
}


You could just use (basing off how they are done in another language):

Code: ags

switch(i)
{
case 1: DoSomething(); break;
case 2: DoSomethingElse(); break;
default: DoUnmanaged(); break;
}

Matti

If you write it like this, it's 6 lines long as well:

Code: ags

if(i==1){
  DoSomething();}
else if(i==2){
  DoSomethingElse();}
else{
  DoUnmanaged();}

Galen

It's still needlessly complex to just choose between three choices depending on the value of a variable.

Which do you prefer:

MyVar=MyVar+1;
or
MyVar++;

Pumaman

#3
You're not the first person to ask about this. It will get done some day.

Edit by strazer:

Tracker entry

monkey0506

It's also needless for this suggestion to be rising up again. It's been discussed several, several times, and Chris has said he'll get around to it.

The only thing that really makes it any more complex is that you have to type the name of the variable again. But in the case of the switch you have to type "case" each time...

Both have pros and cons. Switch statements are useful. That's why CJ said he'll do it...but on his schedule. There's so many requests, it's not feasible for him to implement everything all at once.

He's always been very dedicated to updating the engine making it faster, better, stronger. With new features though come new bugs. That should also be taken into consideration considering the frequency of updates since the release of 3.0.

Pumaman

Bearing in mind that this was first requested back in 2003, I think it's worth me elaborating a bit on why it still isn't here.

Basically, implementing script compiler changes like this are relatively complex and need a lot of testing to make sure that they haven't broken anything, since the scripting engine is an essential core feature of AGS.

To implement and test "switch" would take a relatively long time of development, and because the workaround of "else if" is so easy, it has simply never been a priority. Every release involves prioritisation, and there have always been other things to add to AGS that have a higher "cost/benefit" than the switch statement.

My long-term plan is to do an internal re-write of the script compiler from scratch to make it better designed and more extensible, and as part of that to add things like "switch", "break", etc. That's a good few months off at the moment, though.

Galen


SMF spam blocked by CleanTalk