Protected variables are evil
I cannot come up with a good reason why anyone would need a non-final protected variable in a class. In fact the term "protected" is really a huge misnomer, it should really be called "less-public" or "not-very-protected" , "abomination" or just plain old "evil".
I've been following this rule for years. If a variable is not final then it should be private. If you need to make it accessible to another class and there is no accessor then you're probably not doing something right. Yet I still see even very experienced programmers fall for the fallacy of protected variables. Why? Why? Why?
All that protected variables provide is a lot of really bad design that later lead to sneaky bugs which are hard to figure out and tend to live inside applications for years before someone stumbles into them "oh gee, that one class didn't realize this silly variable had to be initialized". I say down with protected variables, make them go away so they stop taunting the less experienced programmers with the possibility of easy access and magic values.
One would think this doesn't even need to be said.. but apparently that's not true! Speaking of things that shouldn't need to be said, how many java programmers don't realize that protected access works at the package level? I know of at least two that didn't.
Comments
I don't know. I've been bitten a few times with external components that required subclassing but used private members. This created enormous customisation problems.
I think the problem is more that protected members are not considered part of the clas interface. A protected member is very definitely a part of the public API of a class and should be documented and designed as such. They should be treated in the same was as public API methods. That's the way I do it with my own components.
Posted by: Richard Rodger | March 3, 2006 05:44 AM
If all developers read and follow documentation the world would be a much happier place.. such as it is, it's best to enforce the behaviour if something is required and protected variables don't provide the easiest way of doing that.
Posted by: kasia | March 3, 2006 07:55 AM
now, see, this is where being out of the field is frustrating for me. i used to have an example where protected is actually useful, but i can't remember it. i'd check notes but god knows if i'll have the time.
i also think this example is in C++, not Java and the rules for protected may differ.
i'll let you know if i can find it.
Posted by: ann marie | March 10, 2006 05:42 PM
Correct me if I'm wrong, but without protected fields, you can't do something like this:
class Foo { protected Logger logger = Logger.getLogger(this.getClass()); public Foo() { logger.debug("in constructor"); } } class Bar extends Foo { public doBaz() { logger.debug("in baz"); } }*That's* what I find protected fields useful for. But otherwise, I agree, fields should not be accessable outside of their enclosing classes.
Posted by: blalor | March 13, 2006 03:37 PM