A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Public vs Private w/get-set

  1. #1
    Member
    Join Date
    Feb 2001
    Posts
    56

    Public vs Private w/get-set

    Does anyone know if there's a performance difference or advantages between:
    PHP Code:
    public var somevariable:String 
    and
    PHP Code:
    private var _somevariable:String
    public function set somevariable(invalue:String):void{
       
    _somevariable invalue;
    }
    public function 
    get somevariable():String{
       return 
    _somevariable;

    While I know that in cases where you want other commands, variable settings, statement, or certain subclassing - to run when getting or setting (say an event dispatch) - this can be valuable.
    I'm trying to learn why it would be done the second way (with private) if there are no other commands,etc - just basically making a private variable with public function access.

    The second way seems (to me) to be extraneous, but I'm hoping someone here can explain the wisdom and/or advantages of why it should be done the second way...

    Thanks in advance.

    -<>|TheMadFlasher|<>-

  2. #2
    Senior Member
    Join Date
    May 2004
    Posts
    226
    Personally, I think the get/set is 90% time overkill. Getters/setters are slower, code is be executed as opposed to accessing the value of a pointer. That being said, there are two strong arguments for using getters/setters and I do use them frequently. The first is that they can be defined in an interface, unlike vars and therefore makes casting to interfaces more powerful and enables "var like" access. The second is encapsulation, which I have mixed feelings about, were all your internal vars and methods are private and only the vars and methods you think are needed by the person using the class are exposed. In your example above you would do some validation on invalue before you actually set _somevariable.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center