  | | | checking that an object exists | checking that an object exists 2005-04-07 - By Brad Friedman
Back Bernard Lebel wrote:
>Finally, I should add that although I don't know much about >performance, exceptions in Python is just pure joy (would be long >winded to go through this but let's just say there is so much you can >do in this area it's crazy). It is very tempting in Python to fall >onto this type of flow control. > > Yea. I fell into the same thing with Java. Exceptions are really easy and powerful. Especially when you subclass them.
Don't get me wrong. I'm not suggesting that all exceptions should be forbidden from code. I'm just saying when you are dealing with (or authoring) an API, its not always the best idea to use exceptions for flow control at a low level.
Checking the existence of an object by name could be done maybe 10 times in an average command... in which case throwing 10 exceptions is no big deal.
But if you are managing the state of say, thousands of objects a frame via scripting... you really don't want to be using exceptions for your flow control deep inside the loop. Who knows how many thousands of times its going to be thrown? That'll add up. Now if you are using the exception to inform about an honest to goodness error... go for it. If its not thrown, no harm. But if you are depending on it as a fairly frequent condition, it will hurt your performance.
In this case, the question is: can you think of a situation in which the non-existence of an object is not an error but still needs to be checked frequently? I'd suggest that there are times when that might need to be done. So a method of checking that out that doesn't involve throwing and catching an exception would likely speed things up.
I used to throw exceptions around willy nilly. Then I started working a bit in the video game world and got yelled at for it :) And rightly so (ok not yelled... just told "don't do that!"). Now thats a realtime engine. So performance is paramount. Depending on what you are programming in XSI at any given time, performance may be high or low on the priority list. And that probably will determine your style regarding exceptions for a given project I'd imagine. But if you've got no option because the API you are programming to throws exceptions as the solution to a problem... then you're stuck. Its out of your hands. You have to catch it.
In the Java system APIs, exceptions are used all over the place. And on top of that, the compiler wont let you write a method that doesn't explicitly catch or throw all the exeptions that can be generated by the methods called inside it. It makes for exceptionally stable code. Just not neccesarily the fastest code.
As a side note: the c# compiler (extremely similar to java and python as a OO language) doesn't force you to catch exceptions. but it has a command line flag that optionally forces it into that mode if you want. Its an interesting design choice. I've not turned it on yet but I can't help thinking I should for stability's sake.
-brad --- Unsubscribe? Mail Majordomo@(protected) with the following text in body: unsubscribe xsi
|
|
 |