Author: James Courtier-Dutton Date: To: Hampshire LUG Discussion List CC: Hants LUG Subject: Re: [Hampshire] Code style
2009/6/7 Hugo Mills <hugo@???>: > If you were writing a function with a fast path and a slow path,
> which style would you use to write the function?
>
>
> Style A:
>
> if can_use_fast_path:
> return "fast path result"
> # do slow stuff
> return "slow path result"
>
>
> Style B:
>
> result = "fast path result"
> if !can_use_fast_path:
> # do slow stuff
> result = "slow path result"
> return result
>
>
> 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? Would you use a different idiom in different languages? If
> so, why?
>
> Discuss. :)
>
> Hugo.
>
I would aim for a function to have a single entry point and a single
exit point with goto's used on the error paths. I.e. the Linux kernel
style.
It would also depend on how "can_use_fast_path" is defined. I.e. Is it
set a program start, or is it set at each function call. If it is set
at program start, I would probably use a function pointer.