Suggestion for "Scope of variable is too broad"

This inspection should ignore coments and whitespace between the
variable declaration and first use. For example:


The inspection suggests to move the foo declaration to the line before
the if statement. I think that's unnnecessary since there's no actual
code in between that suggested position and it's original position.

Ciao,
Gordon

--
Gordon Tyler (Software Developer)
Quest Software <http://www.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: (416) 933-5046 | Fax: (416) 933-5001

18 comments
Comment actions Permalink

Please submit a tracker item. Don't want to lose this one.

--Dave Griffith

0
Comment actions Permalink

I fail to reproduce this. Do you have a small compilable example?

Bas

Gordon Tyler wrote:

This inspection should ignore coments and whitespace between the
variable declaration and first use. For example:

 int foo;
> 
> // blah blah
> 
> if (someConditionIsTrue()) {
>    doStuffWithFoo(foo);
> }
> 
> doOtherStuffWithFoo(foo);
> ]]>


The inspection suggests to move the foo declaration to the line before
the if statement. I think that's unnnecessary since there's no actual
code in between that suggested position and it's original position.

Ciao,
Gordon

0
Comment actions Permalink

Issue submitted with simple class which reproduces the problem:

http://www.jetbrains.net/jira/browse/IDEA-4626

Ciao,
Gordon

--
Gordon Tyler (Software Developer)
Quest Software <http://www.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: (416) 933-5046 | Fax: (416) 933-5001

0
Comment actions Permalink

Gordon Tyler wrote:

Issue submitted with simple class which reproduces the problem:


Unfortunately I still can't reproduce it on your simple class (which does not compile, by the way). I even reinstalled build #3436 for the occasion to see if I inadvertently fixed something since that build, while I was making other changes. Sorry, but I had to close your request as not reproducible.

Bas

0
Comment actions Permalink

Sorry, it does compile, but IDEA seems to mark it red in some cases (can't reproduce it consistently yet).

Bas



Attachment(s):
red_code.png
0
Comment actions Permalink

Sorry, that was a hurried and contrived example although it did work for
me. Maybe our Error settings are different. Anyway, here's an example
which should work fine and demonstrate the problem:

public class TestVariableScope {

public static void main(String[] args) {
int foo;

// blah
if (args != null) {
foo = args.length;
} else {
foo = 0;
}

System.out.println(foo);
}

}

--
Gordon Tyler (Software Developer)
Quest Software <http://www.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: (416) 933-5046 | Fax: (416) 933-5001

0
Comment actions Permalink

Gordon Tyler wrote:

Sorry, that was a hurried and contrived example although it did work for
me. Maybe our Error settings are different. Anyway, here's an example
which should work fine and demonstrate the problem:


I really sorry, but even with your new example, I still can't reproduce
it. Does anybody else see this problem?

Bas

0
Comment actions Permalink

Something else that I noticed is that if I add an initializer to the foo
declaration (even just initializing it to 0), the inspection does not
trigger. Only when it is an uninitialized variable does the inspection
trigger with comments after it.

Ciao,
Gordon

--
Gordon Tyler (Software Developer)
Quest Software <http://www.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: (416) 933-5046 | Fax: (416) 933-5001

0
Comment actions Permalink

If I copy & paste the example, then I do not see the problem.

However, if I type it out by hand, then I do see the problem, and it doesn't even seem related to comments.

Here is a smaller example without the comment. If I
first create the class, then create the main method,
then type "int foo;" and then the if statement, when I have finished the if statement, then foo is hilighted.

public class TestVariableScope
{
public static void main(String[] args) {
int foo;
if (args != null) {
foo = args.length;
}
}
}

The problem happens whether or not there is whitespace or comments between the declaration of foo and the if statement.

HTH
-Alex



Attachment(s):
idea3459_scope_of_variable_too_broad_hilighting_error.gif
0
Comment actions Permalink

The problem happens for me even if I initialize foo using the smaller example I gave above.

0
Comment actions Permalink

Sorry, I still don't see the problem. This is exactly the case this
inspection was meant to highlight. "foo" is only used inside the if
statement, and so it could be moved there (using the quick fix).

Bas

Alex wrote:

If I copy & paste the example, then I do not see the problem.

However, if I type it out by hand, then I do see the problem, and it doesn't even seem related to comments.

Here is a smaller example without the comment. If I
first create the class, then create the main method,
then type "int foo;" and then the if statement, when I have finished the if statement, then foo is hilighted.

public class TestVariableScope
{
public static void main(String[] args) {
int foo;
if (args != null) {
foo = args.length;
}
}
}

The problem happens whether or not there is whitespace or comments between the declaration of foo and the if statement.

HTH
-Alex


------------------------------------------------------------------------

0
Comment actions Permalink

I had a question about this. Typically, I write loops that initialize objects
like this:


However, after using this inspection for a little while, I noticed that it
was recommending a structure like this:

for (int i = 0; i < someArray.length; i++)
{
SomeObject x = new SomeObject();
someArray+ = x;
// do some work on x;
}


I did some performance tests and I was surprised to see the second snippet
perform substantially better. I guess I always naively thought that the
first would be faster, since there was really only 1 "x" ever created. That
doesn't seem to be the case, however. Can anyone explain why the second
snippet performs so much better than the first?

(i have some ideas, but i'd like to hear from others)

Tobin


0
Comment actions Permalink

The space for the variables is allocated on the stack once on entering
the method. The only change that I can imagine at this moment this
different style will cause is the order in which the variables are
allocated on the stack. It's strange that that would have a large
performance effect. Thus the only explanation I have is that it isn't
caused by the scope change:-) But I could be wrong.

Bas

Tobin Juday wrote:

I had a question about this. Typically, I write loops that initialize objects
like this:

 SomeObject x;
> for (int i = 0; i < someArray.length; i++)
> {
>     x = new SomeObject();
>     someArray+ = x;
>     // do some work on x;
> }
> ]]>


However, after using this inspection for a little while, I noticed that it
was recommending a structure like this:

for (int i = 0; i < someArray.length; i++)
{
SomeObject x = new SomeObject();
someArray+ = x;
// do some work on x;
}


I did some performance tests and I was surprised to see the second snippet
perform substantially better. I guess I always naively thought that the
first would be faster, since there was really only 1 "x" ever created. That
doesn't seem to be the case, however. Can anyone explain why the second
snippet performs so much better than the first?

(i have some ideas, but i'd like to hear from others)

Tobin

0
Comment actions Permalink

Shorter variable lifetimes -> easier time for the JIT register coloring algorithm -> better mapping of locals to registers?

Shorter variable lifetimes -> more opportunities for aggressive garbage collection -> fewer objects go into longer-term heap sections -> lower total garbage collection time?

Simpler code -> more aggressive compilation by the JIT compiler -> less interpretive overhead?

None of those feel right to me, but it's probably something along those lines. The ways of JVMs are complex and difficult to guess at.

--Dave Griffith

0
Comment actions Permalink

Alex wrote:

public class TestVariableScope
{
public static void main(String[] args) {
int foo;
if (args != null) {
foo = args.length;
}
}
}


This is not the case I'm talking about. In my case, the foo variable is
used after the if statement as well, so it can't be moved inside the
if block. However, if there is a comment between the declaration of foo
and the if statement then the suggestion is to move the declaration
after the comment but before the if statement and that's what I think is
spurious.

Ciao,
Gordon

--
Gordon Tyler (Software Developer)
Quest Software <http://www.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: (416) 933-5046 | Fax: (416) 933-5001

0
Comment actions Permalink

Alex wrote:

The problem happens for me even if I initialize foo using the smaller example I gave above.


As I said in my reply to your other message, your example does not use
'foo' after the if statement, so the inspection is correctly suggesting
that the declaration be moved inside the if statement.

Ciao,
Gordon

--
Gordon Tyler (Software Developer)
Quest Software <http://www.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: (416) 933-5046 | Fax: (416) 933-5001

0
Comment actions Permalink

Oops. Sorry for the false positive.

I did some more testing with your example, and the only time I can get the "Scope of variable is too broad" to pop up is if I pause after starting a comment character "/". If I pause there, then the warning pops up. But after finishing the comment, e.g. "/* test */", then the warning goes away.


public class TestInspection {

public static void main(String[] args) {
int foo;

/

if (args != null) {
foo = args.length;
} else {
foo = 0;
}

System.out.println(foo);
}
}

-Alex



Attachment(s):
idea3459_scope_of_variable_too_broad_when_syntax_error.gif
0
Comment actions Permalink

Alex wrote:

I did some more testing with your example, and the only time I can get the "Scope of variable is too broad" to pop up is if I pause after starting a comment character "/". If I pause there, then the warning pops up. But after finishing the comment, e.g. "/* test */", then the warning goes away.


Try with a line comment instead of a block comment?

It's odd that nobody else sees this. I've upgraded to IDEA 5.0.1, so it
can't be that I have an out-of-date build.

Ciao,
Gordon

--
Gordon Tyler (Software Developer)
Quest Software <http://www.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: (416) 933-5046 | Fax: (416) 933-5001

0

Please sign in to leave a comment.