by throwawaylinux · 5y ago · 0 comments
int pow2(int v, int pow)
{
int result;
if (pow >= 0)
result = v << pow;
else
result = v >> -pow;
return result;
}
Looking at this, both sides of the if/else perform a multiply by 2^pow, you can't take the operation to possibly not happen.So I agree factoring common operations can be a good thing, but I think it's really a case by case basis and my code is fine.
I would also caution against assuming something may not happen elsewhere in a function just because you see it inside a conditional block. This happens frequently to various degrees and I've never encountered a style policy preventing it or any compiler or linter warnings for it.
No comments yet.