Hey folks, in alot of the JS code I've been working on in mozilla, I'm
seeing some wasteful patterns that I just thought I'd bring up.
I see alot of
var foo = Components.classes['...'].createInstance();
if (foo) ...
and
var foo = foo.QueryInterface(...)
if (foo) ...
and important thing to know here is that both the
createInstance/getService and the QueryInterface will THROW EXCEPTIONS
if they fail. This means that "if (foo) .." will ALWAYS evaluate to
true, because if the previous statement failed, if (foo) would NEVER be
executed. it just makes it harder to read, and slows down your code.
Anyway, the other thing to note is that if you are EXPECTING an error,
then use try/catch to catch the error. If you don't expect it to fail,
let the exception be thrown and let some other caller handle it.
don't do stuff like this either:
try {
...
} catch (ex) {
return;
}
unless you have the very specific goal of masking a failure!
the failure of a try/catch always results in a return, and a bubbling up
of an exception to the caller. the only thing the above pattern does is
hide the error from the caller.
Just my 2 cents..
Alec