tags:

views:

96

answers:

3

Suppose, I have a method with a parameter of Object type. The method returns nothing - void.

First it checks whether the parameter is not null (or any other check, like objectParam.isEnabled())

if (objectParam.isEnabled()) {
    // ok
}

Now, if the condition is satisfied, I need local variables. If it's not, then I don't need any variables.

WHERE SHOULD I define them? Inside the "if scope" or just after the method header?

Of course, I can do it wherever I like, but which way should be a better practice?

+13  A: 

I believe it's best practice to declare a variable as late as you can, in the most tightly nested scope that you can, ideally at the point where it's initialized with a useful value.

That makes it clearer where and how it's going to be used - when you're looking at the code where it's used, you won't have to look up very far to see the declaration.

In this particular case I disagree with the official Java Style Guide - and so does Josh Bloch. From Effective Java, 2nd edition, item 45:

The most powerful technique for minimizing the scope of a local variable is to declare it where it is first used.

So if you don't need the variable until you've executed a few other statements, don't declare it until that point.

Jon Skeet
@Jon Skeet. Then I got a question.You wrote: "ideally at the point where it's initialized with a useful value."What if I have a loop. If it's never executed [for example, no elements in the list], I will not need any of the variables. (But! I will surely need them in the case when there'll be at least one element in the structure I want to process).If I declare them outside the scope of the loop, I will not follow your advice.If I declare them INSIDE THE LOOP, they will be redeclared on every iteration.Is it OK ? Maybe JVM optimizes the code and that is not a problem?
EugeneP
You should declare them inside the loop if they're only needed in the loop, unless there's some really good reason not to - e.g. you only really need to initialize once, and that initialization is expensive. Note that just *declaring* a variable inside the loop doesn't have a runtime performance penalty.
Jon Skeet
does not have a runtime penalty? Interesting.So you mean it is better to program like this:<pre>while (rs.hasNext()) { String name = rs.getString("name"); String surname = rs.getString("surn"); // do something with those local vars // ...}and not like that:</pre><pre>String name, surname;while (rs.hasNext()) { name = rs.getString("name"); surname = rs.getString("surn"); // do something with those local vars // ...}</pre>?
EugeneP
Absolutely - the first way is much better, and just as fast.
Jon Skeet
+1  A: 

If they're only used within the

if (...) { }

block then they should be declared at the top of that block.

The Java Style Guide has more details.

MHarris
+1  A: 

You should initialize them inside the narrowest scope where they are used, so inside the if block in this case.

void foo(Object obj){
    if (obj != null) {
        int a = 0;
        ...
    } 
}

If they aren't going to be used anywhere outside this block, there's no need to clutter up the method and confuse readers of your code with extra variables declared outside their required scope.

Bill the Lizard