The Source for Java Technology Collaboration
User: Password:



Editor's Daily Blog

The Evil Umpire

Posted by daniel on September 09, 2003 at 07:01 AM | Comments (4)

Allen Holub has found himself writing an "is evil" series. Holub has long provided advice in terms that those that agree would characterize as "clear and concise" and those that disagree would characterize as "strongly worded." Past targets have included thread abuse, and the extends keyword. His latest article returns to a recurring theme of his on the overuse and misuse of accessor methods.

In Holub's JavaWorld article Why getter and setter methods are evil he argues that "they are not particularly object oriented (OO). In fact, they can damage your code's maintainability." One problem is that getters violate data abstraction.

[T]here might be 1,000 calls to a getX() method in your program, and each call assumes that the return value is of a particular type. You might store getX() 's return value in a local variable, for example, and that variable type must match the return-value type. If you need to change the way the object is implemented in such a way that the type of X changes, you're in deep trouble.

Holub suggests that you examine what drove you to use a getter in the first place. To reduce the number of accessors, he advises, "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." There are times that a getter is appropriate - but Holub doesn't consider these getters to be accessors.

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.

You wouldn't think of a factory method as an accessor. You probably also wouldn't think of the get method for a Singleton as an accessor - but Holub probably has an article on deck titled "Why Singletons are evil". Holub considers the getX() setX() convention in JavaBeans. First he points out that you can create JavaBeans without getters and setters. Second, he argues

Accessors were created solely as a way to tag certain properties so a UI-builder program or equivalent could identify them. You aren't supposed to call these methods yourself. They exist for an automated tool to use. This tool uses the introspection APIs in the Class class to find the methods and extrapolate the existence of certain properties from the method names. In practice, this introspection-based idiom hasn't worked out. It's made the code vastly too complicated and procedural. Programmers who don't understand data abstraction actually call the accessors, and as a consequence, the code is less maintainable. For this reason, a metadata feature will be incorporated into Java 1.5 (due in mid 2004).

In the Also today section we also link Andy Lester's essay The computer that cried "Wolf!". Lester begins with a list of the warnings we ignore in everyday life and then moves on to our computer habits that need to be reexamined. His advice is simple - don't add to your work-related mental clutter. "Deal with every problem. Don't brush the problem aside by making a mental note to deal with it later. That method doesn't scale. In fact, NOTHING that relies on a single person scales. Make the computer do its work."

In today's featured Weblogs java.net community manager John Bobowicz recommends Socializing development tools by integrating "social software, like weblogs, wikis, RSS feeds, Instant Messangers, etc, into the development environment." The talkback already includes objections to this tendency towards "Kitchen-sink apps." Jimothy suggests that "we should instead focus on making applications easier to integrate with one another."

From the Java Today News Page, news editor Steve Mallett, has gathered the following News Headlines .

  • Apache Geronimo: "Healthy and Vibrant"
  • Java 1.4.1 Update 1 Released for Mac OS X
  • Tapestry 3.0-beta-3 Released
  • submission form. All submissions go through an editorial review before being posted to the site. You can also subscribe to the java.net News RSS feed.

    This blog is delivered weekdays as the Java Today RSS feed. Once this page is no longer featured as the front page of Java Today it will be archived at http://today.java.net/today/archive/index_09092003.html. You can access other past issues by changing the address appropriately.


    Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
    Comments
    Comments are listed in date ascending order (oldest first) | Post Comment

    • Is Allen being realistic?
      I read Mr. Holub's article, and then a 1999 article he wrote for JavaWorld ( http://www.javaworld.com/javaworld/jw-07-1999/jw-07-toolbox_p.html ) and I can't shake the feeling that what he proposes isn't very practical.

      Generally speaking, I just haven't found a good way to represent my objects in a UI without accessors (or their equivalent). How, for instance, am I to display an HTML table of results, perhaps along with a graph of the same data? His 1999 article says "All objects must provide their own UI." Should I have a getHTMLTable() method that returns an HTML String to render in the browser? And then a getGraph() method to display a graph? Alarms immediately go off in my head about mixing the model and the view. But wait! The article then characterizes MVC as a non-object oriented approach. Allen doesn't call MVC evil, but he does say, "Everything you know is wrong!"

      So, is he right? Is everything I know actually wrong? Have I been brainwashed into thinking MVC is a maintainable, scalaby, and useful architecture? Or is Allen being unrealistic?

      Posted by: jimothy on September 09, 2003 at 10:52 AM

    • Possible and sometimes practical, but...
      So the article suggests that it is better to do something like:
      humanUI.render(MyTextBox);
      than:
      MyTextBox.setText(human.getHairColor());

      The first case is better, because it is less coupled. Adding an adapter class that extends the application layer object and implements a UI layer interface makes the program more flexible.

      This technique can actually be used with Swing interfaces , such as TableCellRenderer, TableCellEditor and TableModel, to implement the MVC design.

      But things get complicated quickly. Without multiple inheritance, it's hard to use this technique while taking advantage of the builtin swing helper classes, such as AbstractCellEditor. The only option is use containment or aggregation to benefit from the helper classes( which maybe not a bad thing).

      The second issue is that you are forced to use protected data members, because with out accessors, you'll need a way for your derived classes to access the base class information. Besides making the inheritance heirarchy more brittle, there is a lost benefit that the accessors provide.

      Accessors provide an opportunity to check pre & post conditions, handle errors and validate parameters. Using protected accessor methods and private data members seems to bring the best of both worlds.

      As the article mentions, sometimes there's no getting around it. Most programmers probably use multiple paradigms and paradigms bleed over to one another, just like aspect -oriented programming needs oop, and java has a main function to get things started.


      Posted by: d_bleyl on September 15, 2003 at 11:42 AM

    • 网络营销软件
      网络营销软件
      网络营销软件
      群发软件
      群发软件
      ---
      群发软件
      网络营销软件
      论坛群发软件
      网站排名软件
      群发软件
      推广小助手破解版
      论坛群发软件
      网站排名软件
      群发软件
      网络营销软件
      网站推广软件
      信息群发软件
      论坛群发软件
      信息群发软件
      博客群发软件
      qq群发软件
      邮件群发软件
      博客群建软件
      企业名录搜索软件
      信息群发软件
      邮件群发软件
      论坛群发软件
      博客群发软件
      网站推广软件
      网络营销软件
      全能营销破解版

      Posted by: 9uf009 on December 14, 2007 at 12:10 AM

    • wow power leveling
      wow powerleveling
      wow power leveling
      wow gold
      wow items
      feelingame.com
      wow tips
      Most Valuable WOW Power Leveling Service
      wow power leveling faq
      cheap wow power leveling
      wow power leveling
      wow powerleveling
      wow power lvl

      Posted by: wowleveling3 on December 14, 2007 at 01:13 AM





    Powered by
    Movable Type 3.01D
 Feed java.net RSS Feeds