I don't know if the code with the if odd(n)
statement is more efficient or more robust - probably neither.
First, you now have two branches you have to worry about, and twice as many statements you can introduce bugs into. Also, if you decide to calculate n_long
and n_short
differently, you can then unintentionally write code that behaves differently for odd and even n
.
Second, code with branches may run more slowly if it's too large and doesn't all fit in the cache.
I prefer to write code, as in my example above, that works in both cases. It costs more time to write it, and something like a proof of correctness, but I have what it takes, and once I have the code, I can use it and rely on it without further ado. Shorter code is also easier to copy and paste.