The Artima Developer Community
Sponsored Link

Agile Buzz Forum
getters/setters

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
getters/setters Posted: Jan 6, 2005 5:21 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by James Robertson.
Original Post: getters/setters
Feed Title: Cincom Smalltalk Blog - Smalltalk with Rants
Feed URL: http://www.cincomsmalltalk.com/rssBlog/rssBlogView.xml
Feed Description: James Robertson comments on Cincom Smalltalk, the Smalltalk development community, and IT trends and issues in general.
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From Cincom Smalltalk Blog - Smalltalk with Rants

Advertisement

Blaine Buxton discusses whether or not to use getters/setters (as opposed to direct variable access). His post is based off this article, which opposes using them.

Blaine comes down as a pragmatist, stating that the most important thing is consistency - either use them or don't, but don't mix and match styles. I have a slightly different perspective, but it's colored by my experience developing BottomFeeder. One of the things I added early was the ability to have the app update itself on the fly. Smalltalk has always been capable of this, so I thought it would be a nice way of tooting that horn. Using direct variable access is problematic if you are going to update an app on the fly. Why?

Well, posit a method foo that directly accesses a variable bar - the older version (i.e, the one being replaced) was written before that variable had been added to the class in question. So I access the variable... and as soon as I try to send a message to it I'm likely to get an exception. When I add a new variable to a class, it gets initialized to nil, so I have one of two choices:

  • I can wrap all direct accesses with a nil check
  • I can simply add a getter method that does lazy initialization

The former is a more annoying version of the latter, and scatters the same initialization logic all over the class - which violates the once and only once principle. Now, this kind of thing never comes up in Java, because you can't add attributes to a class at runtime there - you would need to restart. For that reason, Java developers simply aren't going to come across this messy little use case for getters. That's why I tend to use getter methods - it makes runtime updating of code far, far simpler.

Read: getters/setters

Topic: Gamers Thumb? Previous Topic   Next Topic Topic: The earthquake

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use