I found a minor bug in the introduce variable refactoring that actually can
broke your code. Nothing serious though, but some cases IDEA creates invalid
code. Consider following guardian statement:

if (index >= 0 && index < 8)
        value = array[index*2]; // array size of 16, we are only interrested in
even cells

Now introduce variable from "index*2", which becomes:

int i = index*2;
if (index >= 0 && index < 8)
        value = array[i];

There's nothing wrong in it, though "i" is calculated always, instead of
when necessary.
Now, if we try to introduce variable from "array[i]" (which is kind of
stupid, but still very reasonable), IDEA first suggests variable named "i",
which is already in use!

Why doesn't the IDEA scan the current environment and suggest unused name?
The user doesn't always know what variables are in use and what are not
(consider long method). "i" for example is a very common name for an integer
variable. Nevermind, let us choose more appropriate name like "cell". It
becomes:

int i = index*2;
int cell = array[index];
if (index >= 0 && index < 8)
        value = cell;

This piece of code is now broken, you can clearly see why: the array is
accessed BEFORE the guardian statement.

All this can be avoided if IDEA tries to introduce the variable to smallest
enclosing symbol environment. It should also scan the environment to find
unused name for the variable. This would cause the revised refactoring steps
to look like:

int i = 64356;
if (index >= 0 && index < 8)
        value = array[index*2];
Object cell = new Object();

=>

int i = 64356;
if (index >= 0 && index < 8)
{
        int i2 = index*2;
        value = array[i2];
}
Object cell = new Object();

=>

int i = 64356;
if (index >= 0 && index < 8)
{
        int i2 = index*2;
        int cell2 = array[i2];
        value = cell2;
}
Object cell = new Object();

A lot of unnecessary but working code. It is compiler's duty to optimize it.
I added variables "i" and "cell" to emphasize how the scanner should work.
Though, following code is still valid but less readable ("cell2" renamed to
"cell"):

int i = 64356;
if (index >= 0 && index < 8)
{
        int i2 = index*2;
        int cell = array[i2];
        value = cell;
}
Object cell = new Object();

----

Well, that's it. Add it to the list of bugs if it isn't already in there.
And by the way, when can we expect the exe launcher?-)

______________________________________
Sampsa Lehtonen
Software Designer
Enlightment Entertainment Ltd.
mailto:[EMAIL PROTECTED]
http://www.enlightment.fi
office: +358-(0)9-774 1255, mobile: +358-40-750 8126


_______________________________________________
Eap-list mailing list
[EMAIL PROTECTED]
http://www.intellij.com/mailman/listinfo/eap-list

Reply via email to