Access restrictions

Started by
1 comment, last by OrangyTang 15 years, 3 months ago
I have been working on a Flash game in ActionScript 3 for a few days. I work in an Agile fashion, without up-front planning of what is going into the game. Still, as I write it, some architectural elements are falling into place. An interesting one is (theoretical) misuse of the access restrictions on class members, because a lot of my classes have public member variables. For instance, my "player" objects have a current position (where they are) and a target position (where they are going), both of which could be modified from the outside without any kind of restriction, so I just made both of them public member variables. Several other classes have properties that can be read from the outside. For instance, my game object has a player sub-object which obviously should not be modified by anyone else, but can be read by the display object in order to be rendered. I'm pretty much able to enforce a consistent practice (such as not allowing the View to change the Model) so I choose to make the player sub-object a public member variable. I could have written accessors, but I suppose that writing them would have taken longer than just making the variables public. Of course, correcting them later on might take longer, but I'm not really convinced that I will need to correct them. Can I hear your thoughts and opinions on this matter?
Advertisement
I pretty much gave up on writing accessors just for the sake of "encapsulating" my data, unless I really needed them. Most of the time I didn't need them though, since the accesors ended up being thin wrappers that called MyMember = value; or return MyMember;. And the 10% of the time that I did need an accessor, I much rather would have fixed up a variable reference right then and there using modern, high-tech tools like Find & Replace (with regex if I was feeling clever) instead of having wasted the time writing and referencing a useless accessor everywhere for that 10% of data.

Of course there are still some things that need accessors, like if I actually need to hide the data. I also like putting strict read-only data behind an accessor, as well as data that has a lot of side effects when it's set/get (duh). But other than that and a few other cases I happily use public member variables and enjoy getting work done faster :)
While I'm a big fan of doing the simplest thing that can possibly work, I find that it's so trivial to add accessors in the first place that I generally avoid starting with public member vars to avoid the big refactoring later. Is adding accessors really that much of a timer saver for you?

Alternatively, when I'm feeling really lazy I skip the accessors but make a point of adding them in as soon as I've got more than one external class referencing them, since then I've proved that I actually need the accessor and it's still pretty easy to do the refactoring.

This topic is closed to new replies.

Advertisement