«    »

Local Variable Declarations

When coding in Java, I prefer to declare local variables at their point of use. On a recent project, I have encountered developers who have been taught in their computing science course(s) to put local variable declarations at the start of the method (when using Java). Back in the days of C this was a requirement of the language. When C++ came along, this restriction was dropped. My guess is that the restriction existed in C to make it easier to write the compiler, but I don't really know. I also don't know why professors are teaching to declare local variables up front. I hope they have some good reasons, and are not just a legacy of them having done lots of C programming.

As a professional software developer, I believe it is important to be aware of and able to justify our practices. Here are my reasons for declaring local variables at their point of use:

  1. Code is easier to read. There are fewer lines of codes, which means it is easier for a single method to fit entirely on the screen. When I see the local variable first being used, I can see what type it is since the declaration is right there, as opposed to having to scroll to the start of the method.
  2. Code is easier to write. When adding some code involving a new local variable, declaring the local variable at the cursor is much easier than scrolling back to the top of the method to add it.
  3. The inline local variable refactoring in Eclipse can be performed. Eclipse does not allow this refactoring when the variable is declared separately and not initialized, or is initialized twice.

When writing this article, I had some additional justifications for my approach that turned out to be invalid. I thought that declaring local variables earlier than their point of use would make it more difficult to perform the extract method refactoring in Eclipse, but I checked and it doesn't make a difference. I also thought that my approach is more consistent with the Sun Java code conventions, but I was wrong about that as well. The relevant lines from this code conventions document are: "Try to initialize local variables where they're declared." and "Put declarations only at the beginning of blocks. Don't wait to declare variables until their first use." Neither approach in this article is fully consistent with the Sun conventions.

Below are code examples of the two approachs.

Example 1: Method with local variable declarations at point of use.

public static Object clone(Serializable target) {
  Assert.notNull("target", target);

  try {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputer = new ObjectOutputStream(byteOutputStream);
    objectOutputer.writeObject(target);
    objectOutputer.close();
    byteOutputStream.close();

    ByteArrayInputStream byteInputStream = new ByteArrayInputStream(
      byteOutputStream.toByteArray());
    ObjectInputStream objectInputer = new ObjectInputStream(byteInputStream);
    Object clonedTarget = objectInputer.readObject();
    return clonedTarget;
  } catch (Exception e) {
    throw new RuntimeException("Error cloning object via serialization.", e);
  }
}

Example 2: Revision of the method in example 1 with all local variables declared at the start of the method:

public static Object clone(Serializable target) {
  ByteArrayOutputStream byteOutputStream;
  ObjectOutputStream objectOutputer;
  ByteArrayInputStream byteInputStream;
  ObjectInputStream objectInputer;
  Object clonedTarget;

  try {
    byteOutputStream = new ByteArrayOutputStream();
    objectOutputer = new ObjectOutputStream(byteOutputStream);
    objectOutputer.writeObject(target);
    objectOutputer.close();
    byteOutputStream.close();

    byteInputStream = new ByteArrayInputStream(
      byteOutputStream.toByteArray());
    objectInputer = new ObjectInputStream(byteInputStream);
    clonedTarget = objectInputer.readObject();
    return clonedTarget;
  } catch (Exception e) {
    throw new RuntimeException("Error cloning object via serialization.", e);
  }
}

What is your preference?

If you find this article helpful, please make a donation.

One Comment on “Local Variable Declarations”

  1. For those worried about violating Sun’s coding conventions, check out this quote by Josh Bloch: “Bluntly put, I think the Code Conventions doc is wrong on this point. More generally, I wouldn’t take that document too seriously. It is not actively maintained or used at Sun.” from http://web.archive.org/web/20041209210459/java.sun.com/developer/community/chat/JavaLive/2002/jl0212.html

«    »