Is it bad practice to initialize a variable to a dummy value?

Posted by froadie on Stack Overflow See other posts from Stack Overflow or by froadie
Published on 2010-05-05T14:33:16Z Indexed on 2010/05/05 14:38 UTC
Read the original article Hit count: 132

This question is a result of the answers to this question that I just asked.

It was claimed that this code is "ugly" because it initializes a variable to a value that will never be read:

String tempName = null;
try{
    tempName = buildFileName();
}
catch(Exception e){
    ...
    System.exit(1);
}
FILE_NAME = tempName;

Is this indeed bad practice? Should one avoid initializing variables to dummy values that will never actually be used?

(EDIT - And what about initializing a String variable to "" before a loop that will concatenate values to the String...? Or is this in a separate category?

e.g.

String whatever = "";
for(String str : someCollection){
   whatever += str;
}

)

© Stack Overflow or respective owner

Related posts about best-practices

Related posts about variable-initialization