Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Why getter and setter methods are evil

Make your code more maintainable by avoiding accessors

I didn't intend to start an "is evil" series, but several readers asked me to explain why I mentioned that you should avoid get/set methods in last month's column, "Why extends Is Evil."

Though getter/setter methods are commonplace in Java, they are not particularly object oriented (OO). In fact, they can damage your code's maintainability. Moreover, the presence of numerous getter and setter methods is a red flag that the program isn't necessarily well designed from an OO perspective.

This article explains why you shouldn't use getters and setters (and when you can use them) and suggests a design methodology that will help you break out of the getter/setter mentality.

On the nature of design

Before I launch into another design-related column (with a provocative title, no less), I want to clarify a few things.

I was flabbergasted by some reader comments that resulted from last month's column, "Why extends Is Evil" (see Talkback on the article's last page). Some people believed I argued that object orientation is bad simply because extends has problems, as if the two concepts are equivalent. That's certainly not what I thought I said, so let me clarify some meta-issues.

This column and last month's article are about design. Design, by nature, is a series of trade-offs. Every choice has a good and bad side, and you make your choice in the context of overall criteria defined by necessity. Good and bad are not absolutes, however. A good decision in one context might be bad in another.

If you don't understand both sides of an issue, you cannot make an intelligent choice; in fact, if you don't understand all the ramifications of your actions, you're not designing at all. You're stumbling in the dark. It's not an accident that every chapter in the Gang of Four's Design Patterns book includes a "Consequences" section that describes when and why using a pattern is inappropriate.

Stating that some language feature or common programming idiom (like accessors) has problems is not the same thing as saying you should never use them under any circumstances. And just because a feature or idiom is commonly used does not mean you should use it either. Uninformed programmers write many programs and simply being employed by Sun Microsystems or Microsoft does not magically improve someone's programming or design abilities. The Java packages contain a lot of great code. But there are also parts of that code I'm sure the authors are embarrassed to admit they wrote.

By the same token, marketing or political incentives often push design idioms. Sometimes programmers make bad decisions, but companies want to promote what the technology can do, so they de-emphasize that the way in which you do it is less than ideal. They make the best of a bad situation. Consequently, you act irresponsibly when you adopt any programming practice simply because "that's the way you're supposed to do things." Many failed Enterprise JavaBeans (EJB) projects prove this principle. EJB-based technology is great technology when used appropriately, but can literally bring down a company if used inappropriately.

1 | 2 | 3 | 4 | 5 |  Next >

Discuss

Start a new discussion or jump into one of the threads below:

Subject Replies Last post
. procedural versus object oriented thinking
By greid
1 01/09/08 07:07 PM
by MartinHunter
. Brilliant
By daedalus_hammer
0 03/25/07 08:06 PM
by daedalus_hammer
. Ignorance
By Anonymous
2 10/05/06 10:14 AM
by Anonymous
. What a Brilliant Theory
By Albert_E
2 10/05/06 10:14 AM
by Anonymous
. Too much LDS at Berkeley?
By Anonymous
5 10/05/06 10:14 AM
by Anonymous
. thanks for the tip
By Gert
2 10/05/06 10:13 AM
by Anonymous
. Loosely Typed getters surely are not evil
By Faiser
5 10/05/06 10:13 AM
by Anonymous
. Getters and Setters in assignment work
By Anonymous
1 10/05/06 08:32 AM
by Anonymous
. A story of Joe Average ...
By Anonymous
3 10/05/06 08:31 AM
by Anonymous
. very silly article
By Patrick Calahan
1 10/05/06 08:30 AM
by Anonymous
. Things you don't get ... 2nd try ;-(
By Mariano Kamp
4 10/05/06 08:29 AM
by Anonymous
. Disagreed with this concept for a long time.
By Anonymous
14 10/05/06 08:28 AM
by Anonymous
. Have to use 'set'?
By Anonymous
2 10/05/06 07:31 AM
by Anonymous
. This is why OO has failed
By Anonymous
13 10/05/06 07:30 AM
by Anonymous
. Using Javabeans
By Anonymous
0 10/05/06 07:30 AM
by Anonymous
. How do you avoid accessors to get (essential?) info?
By Anonymous
( Pages 1 2 all )
25 10/05/06 07:28 AM
by Anonymous
. Some examples please
By Kris Thompson
15 10/05/06 07:28 AM
by Anonymous
. What about Struts and JFC paradigms?
By Anonymous
3 10/05/06 07:27 AM
by Anonymous
. What about Struts and JFC paradigms
By Anonymous
6 10/05/06 07:27 AM
by Anonymous
. Problems using this approach.
By keon
4 10/05/06 06:08 AM
by Anonymous


Resources