railsware/js-routes — pull request #62 by MattiSG (2013-01-24 13:45:00+00:00)
Title: Replace exception throwing by flag passing in recursive calls

The main method for routes resolution, `visit`, relied on throwing a `ParameterMissing` exception whenever it… well, had a missing parameter ;)

While, in cases where the user explicitly forgot adding a mandatory parameter, this was (and still is) desirable, this same behavior was used as a way to handle optional parts of a route through a `catch` logic.

This raised two issues. The first was an annoying behavior of breaking flow whenever code was debugged on the frontend. The second was one of performance. Exceptions are costly, `try-catch` blocks are costly, and the use of a helper method to handle the cases in which exceptions were there to handle logic prevented tail recursion optimization.

This implementation relies on a “sticky” (i.e. recursively passed) flag to decide whether an exception should be thrown or not (i.e. whether the caller expects the missing parameter to be part of the resolution logic or not).

No tests had to be added since the result was expected to be fully isofunctional to the original implementation, and no tests could be added since this implementation detail could not be tested without injecting global code that would detect caught exceptions, and that would be quite costly (the only solution I have at first sight would be to augment `Error` prototype and detect calls to its constructor, which I don't have the necessary knowledge for at the moment).