My Stuff
Love
Respect
Admiration
My Amps
Links
Archives


Add this feed to a running copy of BottomFeeder

Linux World 2006 Speaker

Sunday, September 14, 2003

 
Getter/Setter Issue

So, I've been following the discussion on the PragProg list and the discussions always make me think. It's one of the last forums left where everything doesn't turn into madness. But, I digress. So, recently this discussion has come up and over the years I've waffled from the use getters/setters when you need them to all of the time. I like using getters/setters all of the time now because it helps when you want to refactor code and you might want to throw away an instance variable. Now, usually, I simply use the getter/setter methods to do something else (forward calls) or at the very least be able to look for references to those methods. I think using getters/setters sets you up nicely for the future. On the other hand, how pragmatic are they? Are they the simplest thing to do? Well, I don't think they complicate matters too much and are not that much more code to write. I also believe you should only expose the getters/setters that need to be. I generally like to have my objects be behavior rich and try at all costs not to expose the getters/setters, but sometimes you just have to. But, now, I'm starting to think that if you have a simple enough object why bother with getters/setters. If you need the added flexibility in the future or if you change the instance variable, then you can simply just change it internally and no one else changes.

I can see the pluses and minuses of each side, but I think I still fall on the use them all of the time side. Just because I like being consistent. And I think being consistent always makes code cleaner and more maintainable. People know when they look at my code that there will always be a getter/setter (and that the setter might do extra things like broadcast change events, check rules, etc) for each instance variable. This makes my code predictable from a maintainability point of view. I also think getters/setters make your code more readable no matter whether the language is Java, Smalltalk, Ruby, etc...

Before I close this entry, I will mention Squeak has a really cool property that will automatically create getters/setters when they are called if they don't already exist. This means while you are writing code, you can define your instance variables and then assume that the first time they are accessed via a getter or setter that the method will be called. Now, if you're being a "good do bee" and writing your tests, then these methods will be automically generated for you the first time you run a test that exercises code that hits that getter or setter. Pretty cool huh? You don't have to explicitly write the code for it! Now, I think that's pragmatic....They even cooler thing is that it should be easy to write code that does this in any Smalltalk! You can attempt the same in Ruby (just insert a method at run-time) or any dynamic language for that matter...Very cool!

Feel free to comment!

Comments




Metalheads Against Racism



This page is powered by Blogger. Isn't yours?