Skip to main content

Declaring variables

Posted by staufferjames on June 25, 2007 at 7:51 AM PDT

A pet peeve of mine is when a variable is declared way before it is needed. It is much easier to understand a piece of code if a variable is declared just before it is needed. That immediately tells me that it isn't used anywhere before that or in any wider scope.

I also find it annoying when a variable is declared with a null value and them immediately assigned a value.

String value = null;
value = "hi";

should be:
String value = "hi";

Note that one reason to do that the first way is if the second way would create a line that is too long, but it could be a made a multi-line statement also.

Do you agree? Are there other things like this that you commonly see and bother you?

Related Topics >>