Inline variable might mess up program flow


I've just come across a situation where using the 'Inline Variable'
refactoring introduced a bug in my code.

Imagine a piece of code going like this:

float oldX = this.X;
this.X = someOtherValue;
if (oldX == this.X) { /* Do something */ }

After using the previously mentioned refactoring, the code would become:

this.X = someOtherValue;
if (this.X == this.X) { /* Do something */ }

Which is of course not the effect that I desired :)

Perhaps a solution would be to check the original code and see whether
or not 'this.X' is updated after being assigned to 'oldX'. That way you
can make sure 'this.X' and 'oldX' are still pointing to the same object,
making the chance for bugs to enter through refactoring less big.

Kind regards,

Please sign in to leave a comment.