Initialisation and Definite Assignment

11 PM October 14, 2003

A neat feature of Java is that it checks local variables are definitely assigned before they are first used. Because of this, it is not always necessary to initialise local variables in their declaration.

For instance, take this Java code snippet, which declares and initialises a variable named forward:


ActionForward forward = new ActionForward();
if (i > 5) {
calculateSummaryData();
forward = mapping.findForward(“big”);
} else {
calculateDetailedData();
forward = mapping.findForward(“small”);
}
return forward;

The initialisation of the forward variable in the first line is redundant; the assigned ActionForward object will never be used. The snippet could be rewritten as:


ActionForward forward; // no initialisation
if (i > 5) {
calculateSummaryData();
forward = mapping.findForward(“big”);
} else {
calculateDetailedData();
forward = mapping.findForward(“small”);
}
return forward;

This second form has several advantages:

  1. It does not create a dummy ActionForward instance that will be discarded.
  2. It protects against some kinds of coding errors. If a change to the code results in the code>forward variable not being definitely assigned, the compiler will flag an error. The compiler could not flag an error if the variable were initialised with a dummy value.
  3. It more correctly captures the intention of the code.

When I first started coding Java (in 1997) all Java programmers seemed to know and use definite assignment. Over the past few years, however, I have seen more and more code that initialises variables unnecessarily. I wonder why this is. Any suggestions?

By alang | # | Comments (4)
(Posted to javablogs, Software Development and Java)

Comments

At 00:45, 15 Oct 2003 yahoo wrote:

Well, unfortunately, if you are initializing variable in _try_ block the compiler may not understand it. I find it easier to assign it to null.

(#)
At 02:03, 15 Oct 2003 Simon Brunning wrote:

Somethimes the *compiler* isn't sure that a variable will definitely assigned, even though *you* are. Bloody thing. Perhaps this puts people into the habit of assigning to null.

Don't get this trouble with Python...

(#)
At 02:10, 15 Oct 2003 Harmen wrote:
if (i > 5) {
    calculateSummaryData();
    return mapping.findForward("big");
} else {
    calculateDetailedData();
    return mapping.findForward("small");
}
// not reached

Or:

String foundmapping;
if (i > 5) {
    calculateSummaryData();
    foundmapping = "big";
} else {
    calculateDetailedData();
    foundmapping = "small";
}
return mapping.findForward(foundmapping);

both versions have less duplicated code.

(#)
At 08:27, 18 Dec 2003 lord wrote:

See the the following link:

http://www.livejournal.com/users/notbrainsurgery/2237.html

Showing unecessary initialization enforced by JLS.

(#)

Add Comment




(Not displayed)






(Leave blank line between paragraphs. URLs converted to links. HTML stripped. Indented source code will be formatted with <pre> tags.)




© 2003-2006 Alan Green