GMWolf
aka fel666
So the roadmap lists support for exceptions. But I believe it would be far better to use error codes, and so I wish to discuss it with the community such that YYg can make the right choice.
The problem I have with exceptions is that it completely messes up the code flow.
Take the following example:
If one of the sprite_get statement fail, who knows which one failed? This can be a problem if we later have to free the sprites.
This doesn't apply to sprites only, but pretty much to any function that would throw an exception.
Other issues include the propagation of exceptions.
If one function can throw an exception, then all function around that have to be wrapped in try blocks.
Even when you know an exception won't be thrown (because you already made the checks) , you still have to wrap functions in try blocks.
All these are avoided by using the far more elegant Error code.
Each method would return an error code, with 0 being successful, and other number indicating different errors.
This means we can use normal flow control (if/switch statements) to handle our errors.
"What about functions that return values?" I hear you ask. "How can you both retuurn a value and an error?".
Well the solution is simple: Multiple return values!
Take the go approach!
Let scripts and function return multiple values.
Sonadding a sprite goes from:
To
This is (imo) much cleaner, and make it much easier to follow the control flow.
The problem I have with exceptions is that it completely messes up the code flow.
Take the following example:
Code:
try{
spriteA = sprite_add(...);
spriteB = sprite_add(...);
spriteC = sprite_add(...);
} catch (ioException) {
//do sokmething
}
This doesn't apply to sprites only, but pretty much to any function that would throw an exception.
Other issues include the propagation of exceptions.
If one function can throw an exception, then all function around that have to be wrapped in try blocks.
Even when you know an exception won't be thrown (because you already made the checks) , you still have to wrap functions in try blocks.
All these are avoided by using the far more elegant Error code.
Each method would return an error code, with 0 being successful, and other number indicating different errors.
This means we can use normal flow control (if/switch statements) to handle our errors.
"What about functions that return values?" I hear you ask. "How can you both retuurn a value and an error?".
Well the solution is simple: Multiple return values!
Take the go approach!
Let scripts and function return multiple values.
Sonadding a sprite goes from:
Code:
try{
sprite = sprite_add(...);
//Some more stuff
} catch(e) {
//Do somehing
}
Code:
sprite, error = sprite_add(...);
if (error) {
//Do somehing
}
//Something else