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 217386 - Introduce Variable refactoring poor new code placement
Summary: Introduce Variable refactoring poor new code placement
Status: RESOLVED WONTFIX
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 7.2
Hardware: PC Linux
: P4 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-08-24 15:48 UTC by mclaborn
Modified: 2013-11-15 16:23 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
Example - before refactoring (9.90 KB, image/png)
2012-08-24 15:48 UTC, mclaborn
Details
Example - after refactoring (12.06 KB, image/png)
2012-08-24 15:48 UTC, mclaborn
Details

Note You need to log in before you can comment on or make changes to this bug.
Description mclaborn 2012-08-24 15:48:03 UTC
Product Version = NetBeans IDE 7.2 (Build 201207171143)
Operating System = Linux version 3.2.0-29-generic running on amd64
Java; VM; Vendor = 1.6.0_32
Runtime = Java HotSpot(TM) 64-Bit Server VM 20.7-b02

When extracting a new location variable from existing code, the placement of the new line of code with the new variable is not very smart.  For example, extracting a variable from a line of code which is immediately preceeded by a comment line.  The new line of code is placed above the comment, after the previous real code statement.  It would be more logical to place the new line of code immediately before the line being refactored.  

Screen shot examples coming shortly.
Comment 1 mclaborn 2012-08-24 15:48:34 UTC
Created attachment 123548 [details]
Example - before refactoring
Comment 2 mclaborn 2012-08-24 15:48:54 UTC
Created attachment 123549 [details]
Example - after refactoring
Comment 3 Ralph Ruijs 2012-09-11 16:27:47 UTC
(In reply to comment #2)
> Created attachment 123549 [details]
> Example - after refactoring

To me this looks correct, but maybe I'm missing something because of the small code snippet. Let's look at the following code sample:

public static void main(String[] args) {
    double pi = 3.14159;

    // Variable pi will never be 0, so no need to check for DivideByZeroException
    double diameter = 123456 / pi;
}


Using Introduce Variable on 123456 should be placed above the comment, as the comment is saying something (or should) about the whole statement. Right?
Comment 4 mclaborn 2012-09-11 19:06:43 UTC
In your example, I would want the new variable to go below the comment, immediately above the statement it is refactored from.
Comment 5 mclaborn 2013-11-15 15:51:57 UTC
What about adding an option for this type of refactoring that allows the user to choose whether the new code goes before the comments or after?

In my case the new code should go after the comment 90% of the time, because my comments tend to apply to blocks of code, not individual lines.
Comment 6 Svata Dedic 2013-11-15 15:57:48 UTC
I don't think it's a typical case that the above-comment applies just to a part of expression (= the extracted part should land after the comment).

In your case, it would be probably more effective to move around the lines after the refactoring is applied than interacting with a dialog (or something similar) popping up. A pre-set option will not solve the problem as each of the extraction may need a different treatment.
Comment 7 mclaborn 2013-11-15 16:23:28 UTC
I'm not sure you've understood what I meant.  I typically use comments to refer to a block (several lines) of code, not just part of an expression.  In that case, it makes sense (to me) for the refactored code to be placed after the comment block.

I can see why you don't think my usage is "typical", but it is certainly typical for me.  I doubt that I am the only person who codes or thinks that way.

I wasn't thinking of an option on a per-use basis, but a global option that would apply to all such refactorings.  That way each user could set the option the way that applies to them most of the time and move the lines around as you suggested when there is an exception.