Name for this antipattern? Fields as local variables
- by JSB????
In some code I'm reviewing, I'm seeing stuff that's the moral equivalent of the following:
public class Foo
{
private Bar bar;
public MethodA()
{
bar = new Bar();
bar.A();
bar = null;
}
public MethodB()
{
bar = new Bar();
bar.B();
bar = null;
}
}
The field bar here is logically a local variable, as its value is never intended to persist across method calls. However, since many of the methods in Foo need an object of type Bar, the original code author has just made a field of type Bar.
This is obviously bad, right?
Is there a name for this antipattern?