Adept Software Development

Adept: (A)pplication (D)evelopment (E)nterprise to (P)ersonal (T)ransition. It is a system I am developing to leverage Enterprise developer skills to produce stand-alone software for other market segments. This is a general software development blog discussing issues about project, architecture, design and development. The emphasis will be in Java, but many of the issues will be more general. Almost all will be technical.

http://marringtons.com

Monday, January 10, 2005

Don't Assume a Getter is Benign

As a development manager, team leader and active developer I get to see a lot of code written by others. One of the most common flaws, as I see it, is the repetitive overuse of getters.
If (myBean.getDate() != null && ! myBean.getDate().equals( today))
    showDate( myBean.getDate());

 

I have seen methods where the same getter has been called more than twenty times as it was needed in complex business logic. There is more than one problem with this:
  1. Even if the getter is benign now - meaning that all it does is retrieve package private data, you can't be sure it will remain so. If the call later causes database access or an RMI call it can cause significant response times.
  2. It makes the code appear far more complex than it actually is, so.
  3. General readability is lowered.
So save yourself a headache – not to mention support staff and possible class clients – and use a local variable. Not only do local variables address the above points, but they also encourage for code self-documentation since the variable name can be more descriptive than a more generic getter.
Date initiationDate = myBean.getDate();

If (initiationDate != null && ! initiationDate.equals( today))
    showDate( initiationDate);

 

0 Comments:

Post a Comment

<< Home