PDA

View Full Version : Access modifiers


FEK315
03-10-2011, 04:54 PM
I am not sure when to use public or private modifiers.
I am kind of looking for a rule of thumb.

Is there a difference in the way you use them between working with a team of devs and building as the sole dev?

Thank you:cool:

Eralmidia
03-10-2011, 10:14 PM
In general, go with private if you can. If other classes doesn't really need access to a method they shouldn't be allowed to access it either.

Now, you could argue that if you're the only dev, it really doesn't matter, but while the structure of a class is very clear while writing it, it might not be if you open up a project you worked on several years ago. Keep as much as possible private and rememeber to document classes as well. It could save you a lot of headache later on :) Besides, even if you are the sole dev, you might want to share a class with others at some point.

FEK315
03-11-2011, 06:56 AM
So private means you can't access it throught the FLA or some one can't refence that class with another .AS file.

Noct
03-11-2011, 01:31 PM
I think your putting more weight on it then it actually holds... Public just means that you could change/call that variable/method from outside the class. Private just keeps that variable static unless it is changed using a setter/getter (or by some internal to the class process).

For example, say you have a "Dog" class. When you create an instance of it, you set the dog's "coatColor" variable to "brown".

var brownDog:Dog = new Dog("brown");

If that variable in the class is set to public, you could do this:

var brownDog:Dog = new Dog("brown");
brownDog.coatColor = "blue";

If that variable was set to private, you'd need to use a setter method that would change that variable from within the class:

var brownDog:Dog = new Dog("brown");
brownDog.setCoatColor("blue");

I honestly have never fully appreciated the point of it, and consequently I tend to make most of my variables public, but if I'm writing a class for a client I'll try to follow best practices; and thats one of them...

The way I understand it, the variable should be private if you aren't going to need to access to the actual variable itself from your timeline (or I should say outside your class). You can still call it/change it using getter/setters, but I believe the practice itself is done more for clarity when reading the script then anything else... it's not like a level of security or anything...

FEK315
03-11-2011, 04:13 PM
This is what I am after, Thank you!