Would it be faster to just put code inside a try-catch block instead of performing various error checks?
For example..
function getProjectTask(projec
Depends on the situation. As galambalazs mentions readability is important. Consider:
function getCustomer (id) {
if (typeof data!='undefined' && data.stores && data.stores.customers
&& typeof data.stores.customers.getById=='function') {
return data.stores.customers.getById(id);
} else {
return null;
}
}
compared to:
function getCustomer (id) {
try {return data.stores.customers.getById(id);} catch (e) { return null; }
}
I'd say the second is much more readable. You tend to get data back like this from things like google's apis or twitter's feeds (usually not with deeply nested methods though, that's just here for demonstration).
Of course, performance is also important, but these days javascript engines are fast enough that nobody's likely to notice a difference, unless you're going to call getCustomer every ten milliseconds or something.