Jump to content


Photo

switch statements and if statements


  • Please log in to reply
1 reply to this topic

#1 Zeddy

Zeddy

    Totally Radical Dude

  • GMC Member
  • 1798 posts
  • Version:GM8

Posted 07 June 2012 - 10:13 PM

switch-statements are broken.

Snippet 1:
switch (aVariable){
case 0: doAThing(); break;
case 1: doAnotherThing(); break;
default: doSomething(); break;
}

Snippet 2:
switch (aVariable){
default: doSomething(); break;
case 0: doAThing(); break;
case 1: doAnotherThing(); break;
}

These two snippets of code do not exhibit the same behaviour, as you would expect them to (and as they do in any other programming language with switch statements I've used.) Instead, in the second snippet the cases for 0 and 1 do not run and instead call the default behaviour.

Yes, I can just put default at the end but what if I want to do this?
switch (aVariable){
default: doAThingFirst();
case 0: followUpWithAThing(); break;
}

At least put it in the manual that default has to be last and have GM throw a compiler error from doing otherwise.


if statements are broken
if (instance_exists(someObject) && someObject.x > 100){
   //This code will crash if someObject does not exist
}
It's a very standard coding practice to do things like this in other languages. The second check has no reason to run if the first one returns false, yet it does. Not only does it lead to crashes the first check is supposed to prevent, but it's an unecessary drain on resources. Again, it also runs counter to the behaviour of other languages.
  • 0

#2 GStick

GStick

    All Secrets Exposed

  • GMC Member
  • 1633 posts
  • Version:GM:Studio

Posted 07 June 2012 - 11:21 PM

I'm pretty sure this was fixed in Studio, don't quote me on that though.

This kind of fall through is kind of a bad practice anyway...
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users