New answers tagged

1

You could turn things inside out like this: function getValue(parameter) { if (active) { try { return valueStore.query(parameter) } catch (e) { // quietly fall back to non-active behavior } } return getRandomValue(); } Now there's no duplication or local exceptions. Should the fall back logic ...


-1

Since you asked out of curiosity, and your question is not tagged with a specific language (even though it's clear you had imperative languages in mind), it may be worth adding that languages supporting lazy evaluation allow for a complete different approach. In those languages, expressions are only evaluated when needed, so you can define "variables" and ...


1

The typical way to simplify this sort of code is to create an "empty" resolved promise that you can use in place of the "real" promise if you don't need to get() the url. I don't use jQuery much, but I believe that would look like this: var promise = condition ? $.get('url') : $.Deferred().resolve().promise(); promise.done(function () { done(); }); ...



Top 50 recent answers are included