Open/closed design principle
by Ruben
A while ago a friend and I were having a discussion about the use the private and protected access control attributes in AS3. While I stated that you should always try to write your code as open as possible as to make it flexible, he said that you should always make your code closed unless it was explicitely designed to be not so.
I think the open/closed design principle makes a good compromise: "open for extension, closed for modification".
What it means is that code should be written so that other programmers can easily add functionality by subclassing your classes (extension), while at the same time 'private' stuff like errors will have to be fixed within the original class (modification).
Comments (read older)
Right on Michael, though my intention has never been to tell anyone that making your vars public or protected was in fact wrong. I’m also not completely sure if I agree that applying the open/closed principle is dangerous by definition, as a matter of fact I reckon that it might in fact be a pretty good point to start from, with of course a line of exceptions to the rule.
My rule of thumb is to use protected for data that simple-access, that is, just holding a value. I use private for fields that are complex-access, that is, has dependencies.
For example, if you were keeping track of a bunch of personal information, protected is fine. If you were keeping track of the length or amount of some chunk of data, private.
That said, most of my stuff I leave protected.
Yeah sounds pretty sensible, thanks man.