Name for this antipattern? Fields as local variables
Posted
by
JSB????
on Programmers
See other posts from Programmers
or by JSB????
Published on 2012-08-31T14:25:52Z
Indexed on
2012/08/31
15:52 UTC
Read the original article
Hit count: 165
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?
© Programmers or respective owner