On 2/13/07, Holth, Daniel C. <[EMAIL PROTECTED]> wrote:

I was wondering what people's thoughts were on the use of "get" and
"set" functions.  I personally have felt that creating functions such
as:

public function get theLetterA(){
        return "a";
}

so the user can simply call someObject.theLetterA are confusing because
the user doesn't know if they are doing a function call or accessing a
public variable.  I prefer writing functions such as:

That's the point, though. They don't actually need to know. If you're
using someone else's class, you don't need to know how it's
implemented; you just need to know the interface.

Are there advantages to using "get" and "set" that I'm not seeing?

You can implement as either a public variable (faster, simpler) or as
getter/setter functions (more options, more security) without changing
the class' interface.

I ask because I was always use getVariable() functions and started
reading ActionScript 3 with Design Patterns and the authors (as well as
many others I've read) are using "get" and "set" functions.

AFAIK, getVariable() is a holdover from Java, which doesn't have properties.

As a final benefit, using properties makes code much more readable. Compare:

obj.property += value;

obj.setProperty(obj.getProperty() + value);

The only time I ever use "getX" as a function name is if I need to
pass one or more arguments (which you can't do with getters) For
example:

public function getItemAt(index:Number):Item {
   return _items[index];
}

--
Mike Keesey
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to