Vic wrote: >> Assume that the fast path is a single expression, and the slow path >> is at least tens of lines of code. Why would you pick one style over >> the other? > > I wouldn't use either of the above. > > Style A has multiple returns from the function. That's one of those things > that's just fine right up until it isn't; code grows as different people > work on it, and sooner or later, you can't see both returns on the same > page. That's when mistakes happen. > > Style B evaluates both fast_path and slow_path results if fast_path is not > applicable. This might be an error (side effects are not mentioned), might > cause other problems (*Why* is fast_path not applicable? Because it > hangs?), but will almost certainly be slower than not evaluating the > fast_path result. > > So, given that we *have* to have one branch operation, I'd do a very simple > > if (fast_path_applicable) { > result = fast_path(); > } else { > result = slow_path(); > } > > return (result); > > It might be dull to read, but it's clear, it's accurate, and it will > compile down to one branch taken - which is optimal for the general case - > unless the particular architecture can do non-branching conditionals. > > Vic. > >
Thanks Vic - you saved me from typing it myself. I use the above style whenever possible. So much easier to maintain in the long run. Simon -- Please post to: Hampshire@mailman.lug.org.uk Web Interface: https://mailman.lug.org.uk/mailman/listinfo/hampshire LUG URL: http://www.hantslug.org.uk --------------------------------------------------------------