accessor pros/cons (was [nycphp-talk] object getter/setter methods)
Christopher Merlo
cmerlo441 at gmail.com
Fri Mar 11 13:37:19 EST 2005
On Fri, 11 Mar 2005 12:56:38 -0500, Allen Shaw <ashaw at iifwp.org> wrote:
> The article says:
>
> By designing carefully and focusing on what you must do rather than how
> you'll do it, you eliminate the vast majority of getter/setter methods in
> your program. Don't ask for the information you need to do the work; ask the
> object that has the information to do the work for you.
I agree with this philosophy, and try to impart it to my (Java)
students. To really *get* OOP, you have to retrain yourself to think
a certain way. (This is slightly easier with my students, most of
whom have never programmed procedurally.) An object should be
self-sufficient, and the user of the object should get the information
needed by doing nothing more than passing a couple of messages to it.
You, the class designer, should have already done all the other work.
I disagree, BTW, with the notion that you can avoid coding the
accessors and mutators, because you never know when someone will want
to use one; however, I do agree that smart class design can avoid the
user's need to call them most of the time.
Like the article said, figure out first what people need to do with
your objects; the rest flows naturally.
--
cmerlo441 at gmail.com
http://www.theyellowbox.com/
More information about the talk
mailing list