Best of 2008: A developer's list

JW blogger Dustin Marx names his top 10 technology events of 2008. Highlights include updates to Java SE 6, runtime support in OpenLaszlo 4.2, and the clash of the titans that occurred early in the year, when Sun acquired MySQL on the same day that Oracle announced its acquisition of BEA. No two lists are alike and it's not too late: What were your top 10 for 2008?

Also see:

Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 62: Explicit and recursive programming in Java

Use Java style and idioms to ease maintenance without sacrificing performance

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
When I write a method in Java, I try to use a form that directly and simply states the intent of the method that I am producing. This is called an explicit programming style. By using this style, not only is it easier for me to revisit and fix my code, but it is also easier for others to do so. Some may argue that when using this form, a compromise must be made in the performance of the code, but this is only true in relatively extreme circumstances such as the inner loops of heavily-used processing code.

Background

As a starting code example on which to build in our discussion below, consider the following method from a utilitty class that operates on the Abstract Windowing Toolkit (AWT) provided with the Java Development Kit (JDK):

1:public class Utils
2: {
3:       static public Frame GetFrame(Component c)
4:       {
5:              if(c instanceof Frame || null==c)
6:                       return c==null ? null : (Frame)c;
7:               return GetFrame(c.getParent());
8:        }
9:}


We are going to look at this method from a number of perspectives, including style, efficiency, and generalization.

First, a simple description of the method: it is responsible for finding the first Frame that contains the given AWT Component. If all of the ancestors of the specified Component are checked and a parent Frame is not found, then the method returns a null value.

To start, let's take a look at the routine line by line from the point of view of style, and to some degree from a performance point of view.

5:      if (c instanceof Frame || null==c)


You may be wondering why we first check if the component "c" is an instance of Frame, if afterward we are going to check if it is null. It seems like a strange ordering indeed, given that if the component reference is null, it cannot be an instance of a Frame! From an efficiency point of view, we know that checking an object reference for a null value takes fewer cycles than applying the instanceof operator, so reversing the order of these comparisons would allow the compiler to shortcut the "or" operation after fewer cycles, in the cases where this is appropriate.

6:    return c==null ? null : (Frame)c;


This line of code above is a bit of a disaster from a style point of view. First, it repetitious because it rechecks the validity of the component reference. Next, it uses the ternary conditional operator, a holdover from C/C++, which many people consider more difficult to read than using a standard if statement. It is also considered bad form by many programmers to use a return statement anywhere in the body of a routine except as the very last statement. These early returns can make code harder to debug, not to mention larger and more complicated in terms of the generated code than if the routine had taken the form of a clean, single return statement. Last, a null pointer is returned without an explicit cast to a Frame -- the Java compiler is left to make this cast on our behalf.

Finally,

7:      return GetFrame(c.getParent());


Generally, when a method calls itself, this is termed recursion. This particular form of recursion -- where the method calls itself as the very last expression before the return statement -- is called tail recursion since the method is "chasing its own tail" so to speak. This use of tail recursion is basically just an expression of code reuse. In other words, the routine is using recursion as a simple and "cheap" way of performing looping -- each recursive invocation of GetFrame() finds the first Frame parent, determines that we have run out of parents without ever finding a Frame, or recursively calls itself again. Unfortunately, in Java, use of recursion is expensive compared to using an explicit looping form such as while loop.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
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