When having a class:
class X
{ int data_;
void f()
{
//shall I cache var data_? by doing
int cached = data_
//and instead of this:
if (data_ >= 0 && data_ < 1000 || data_ < 0 && data_ > -1000)//first version
{
//do something
} else
{
//do somegthing else
}
// have this:
if (cached >= 0 && cached < 1000 || cached < 0 && cached > -1000)//second version
// in my opinion this is bad code
{
//do something
}
else
{
//do somegthing else
}
}
};
Please see comments in code.
I'm asking this question because my accomplice from university states that this kind of code (line 1) is just a bad code. I think that he's talking rubbish but I'm very interested in your opinion on this subject.
Thank you.