This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 195157 - "Create local variable" creates variable at wrong location
Summary: "Create local variable" creates variable at wrong location
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 7.0
Hardware: PC Linux
: P3 normal with 1 vote (vote)
Assignee: Svata Dedic
Depends on:
Reported: 2011-02-08 07:26 UTC by rcasha
Modified: 2013-09-02 14:24 UTC (History)
1 user (show)

See Also:
Exception Reporter:


Note You need to log in before you can comment on or make changes to this bug.
Description rcasha 2011-02-08 07:26:42 UTC
At present, the hint "Create local variable" generally creates the variable at the very beginning of the method, instead of at the block start. There are times when the variable is declared on the same line as the hint however, and I haven't worked out when this is done. 

Local variables should be declared as close as possible to where they're being used, that is in the lowest block that encompasses all uses of the variable. At least this should be configurable.
Comment 1 matthies 2011-02-08 16:19:24 UTC
I agree, and to be more precise, the variable should not be added at the start of the block, but at the latest possible position within the relevant block.

For example in

    void foo() {
        // ... some code not using 'temp'...
        for (x : list)
            // ... more code not using 'temp'...
            // (*)
            do {
                // ... more code ...
                if (temp > 0) {
                    // ... even more code ...
                    temp = temp - delta;
            while (temp >= 0);
            // ... more code possibly using 'temp'...
        // ... more code not using 'temp'...

the variable declaration should be created at (*).