New intention: replace member variable with local variable

I don't know, whether I already asked, but it would be nice, if IDEA (or
one of these cool bundled plugins) could highlight member variables,
which could be turned into a local variable, e.g. when the variable is
only accessed in one method and write-accessed before reading it.

Example:

private double value;

public void foo() {
value = bar();
doSomething(value);
}

Thanks in advance.

Tom

2 comments

It's already on my list for InspectionGadgets, if rather challenging to implement. The actual semantics will be that the member variable is accessed in any number of methods, but always write-accessed before read. The Irida description includes a refactoring to make fields local, and presumably I'll be able to have that as a quick-fix. No promises on ETA, but I'd like to have it before Irida GA's.

As an aside, while this inspection looks pretty straightforward, it is actually not quite safe, because making a field local can change the multi-threaded behaviour of a class, even if it is always written before read in any given method.

--Dave Griffith

0

Thanks Dave,

As an aside, while this inspection looks pretty straightforward, it is actually not quite safe, because making a field local can change the multi-threaded behaviour of a class, even if it is always written before read in any given method.


I have not problems with that, quite every day-to-day refactoring might
introduce threading bugs, e.g. introduce a variable for a number of method
calls.

Tom

0

Please sign in to leave a comment.