Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Why getter and setter methods are evil

Make your code more maintainable by avoiding accessors

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 3 of 6

The lack of getter/setter methods doesn't mean that some data doesn't flow through the system. Nonetheless, it's best to minimize data movement as much as possible. My experience is that maintainability is inversely proportionate to the amount of data that moves between objects. Though you might not see how yet, you can actually eliminate most of this data movement.

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. Most accessors find their way into code because the designers weren't thinking about the dynamic model: the runtime objects and the messages they send to one another to do the work. They start (incorrectly) by designing a class hierarchy and then try to shoehorn those classes into the dynamic model. This approach never works. To build a static model, you need to discover the relationships between the classes, and these relationships exactly correspond to the message flow. An association exists between two classes only when objects of one class send messages to objects of the other. The static model's main purpose is to capture this association information as you model dynamically.

Without a clearly defined dynamic model, you're only guessing how you will use a class's objects. Consequently, accessor methods often wind up in the model because you must provide as much access as possible since you can't predict whether or not you'll need it. This sort of design-by-guessing strategy is inefficient at best. You waste time writing useless methods (or adding unnecessary capabilities to the classes).

Accessors also end up in designs by force of habit. When procedural programmers adopt Java, they tend to start by building familiar code. Procedural languages don't have classes, but they do have the C struct (think: class without methods). It seems natural, then, to mimic a struct by building class definitions with virtually no methods and nothing but public fields. These procedural programmers read somewhere that fields should be private, however, so they make the fields private and supply public accessor methods. But they have only complicated the public access. They certainly haven't made the system object oriented.

Draw thyself

One ramification of full field encapsulation is in user interface (UI) construction. If you can't use accessors, you can't have a UI builder class call a getAttribute() method. Instead, classes have elements like drawYourself(...) methods.

A getIdentity() method can also work, of course, provided it returns an object that implements the Identity interface. This interface must include a drawYourself() (or give-me-a-JComponent-that-represents-your-identity) method. Though getIdentity starts with "get," it's not an accessor because it doesn't just return a field. It returns a complex object that has reasonable behavior. Even when I have an Identity object, I still have no idea how an identity is represented internally.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (15)
Login
Forgot your account info?

In my point of view...By Anonymous on November 6, 2009, 12:33 pmObjects have properties, and they need to be populated when the object is created. Some are properties the outside world needs to know about, some are internal to...

Reply | Read entire comment

Twisting concepts I readBy Anonymous on November 4, 2009, 5:54 pmTwisting concepts I read your both articles and they are rubish. You say that data abstarction is an important priciple. Let's read together the definition of...

Reply | Read entire comment

This is a programming style not OOPBy Anonymous on October 17, 2009, 8:48 pmObject oriented programming is about creating objects that mimic the real world things you are trying to program business solutions for: a database, a customer,...

Reply | Read entire comment

"I'm sorry, but I don't getBy Anonymous on October 1, 2009, 4:55 pm"I'm sorry, but I don't get your assertion that you don't have UI code..." I agree with that... What if you want to migrate from AWT/Swing if you tie your classes...

Reply | Read entire comment

"What about ORM frameworksBy Anonymous on October 1, 2009, 4:48 pm"What about ORM frameworks like Hibernate and IoC containers like Spring? Even when getters are eliminated in the rest of the code, the data Model is where most...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources