Is it generally considered better practice for an object to access its own variables via its getter methods?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Is it generally considered better practice for an object to access its own variables via its getter methods?

Andy Burnett
I noticed that the Seaside tutorial has code such as:

html div class: 'menu';  with: self menuComponent.

Whereas the Scriptaculous demo code does things like this:

html paragraph id: 'position'; with: position.


I.e. they didn't bother adding (or using) a getter for position.  Who is right, or doesn't it matter, or is there another reason why it would be written differently?

cheers
AB

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Is it generally considered better practice for an object to access its own variables via its getter methods?

Randal L. Schwartz
>>>>> "Andy" == Andy Burnett <[hidden email]> writes:

Andy> I noticed that the Seaside tutorial has code such as:
Andy> html div class: 'menu';  with: self menuComponent.

Andy> Whereas the Scriptaculous demo code does things like this:

Andy> html paragraph id: 'position'; with: position.


Andy> I.e. they didn't bother adding (or using) a getter for position.  Who is
Andy> right, or doesn't it matter, or is there another reason why it would be
Andy> written differently?

Clearly, position is an instance var, or perhaps a temporary.  An instance var
may or may not have accessors, depending on whether it is meant to be tweaked
from the outside.  #menuComponent, on the other hand, may be just an accessor,
or it may be a whole pile of code to generate that menu on the fly.  At this
point, it doesn't matter.

Whether internal accesses to instance vars should use accessors instead of
direct access is a subject to debate (read: religious war).  I hope you
haven't accidentally triggered that thread here.  I tend to do the simplest
thing that works, and leave it at that.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Is it generally considered better practice for an object to access its own variables via its getter methods?

Andy Burnett

Andy> I noticed that the Seaside tutorial has code such as:
Andy> html div class: 'menu';  with: self menuComponent.

Andy> Whereas the Scriptaculous demo code does things like this:

Andy> html paragraph id: 'position'; with: position.


Andy> I.e. they didn't bother adding (or using) a getter for position.  Who is
Andy> right, or doesn't it matter, or is there another reason why it would be
Andy> written differently?

Clearly, position is an instance var, or perhaps a temporary.  An instance var
may or may not have accessors, depending on whether it is meant to be tweaked
from the outside.  #menuComponent, on the other hand, may be just an accessor,
or it may be a whole pile of code to generate that menu on the fly.  At this
point, it doesn't matter.

Whether internal accesses to instance vars should use accessors instead of
direct access is a subject to debate (read: religious war).  I hope you
haven't accidentally triggered that thread here.  I tend to do the simplest
thing that works, and leave it at that.

Great, I am all in favour of pragmatism.  Thanks.


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Is it generally considered better practice for an object to access its own variables via its getter methods?

Stan Shepherd
In reply to this post by Randal L. Schwartz
Randal L. Schwartz wrote
Whether internal accesses to instance vars should use accessors instead of
direct access is a subject to debate (read: religious war).  I hope you
haven't accidentally triggered that thread here.
There is a nice discussion of the pros and cons of each in:

http://stephane.ducasse.free.fr/FreeBooks/BestSmalltalkPractices/Draft-Smalltalk%20Best%20Practice%20Patterns%20Kent%20Beck.pdf

Pages 68-69.

My summary - direct for clarity, indirect for inheritance.