Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Tolls

Thread: Finding where variable is defined and where referenced

  1. #1
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Finding where variable is defined and where referenced

    Is there a way to select a variable in your code and ask the IDE to tell you where it is defined and where it is referenced?

    There are many students that have shadowed variables. Is there a way to use the IDE to find the variables that are hiding variables at larger scope.

    Why doesn't the IDE warn you of this?

    When do you ever want to have more than one variable with the same name at different scope levels?

    Thanks,
    Norm

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Finding where variable is defined and where referenced

    I've not used it myself, but many seem to use FindBugs with Eclipse to help pro-actively root out bugs, and it appears to have some facilities to identify shadowing (though again have not tried it myself).

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Finding where variable is defined and where referenced

    It seems like a lot of OPs could use a warning when they shadow a variable.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Finding where variable is defined and where referenced

    Quote Originally Posted by Norm View Post
    It seems like a lot of OPs could use a warning when they shadow a variable.
    I don't disagree as I've been burned by this many times. I wonder if the other IDE's, such as NetBeans and IntelliJ Idea, do better at this.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Finding where variable is defined and where referenced

    Norm, you must have a crystal ball that works: writing-textfile-jtextarea.html

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Finding where variable is defined and where referenced

    I see problems like that every day. Its a sure bet there will be lots more to come. I was wondering if the wonderful IDEs had a tool that would help show the OPs their problems.

    In the old days with paper cross reference listings, all the variables were listed with the line they are defined on and the lines where they were referenced. If you saw a variable that was not referenced or one that was defined in two places, it was usually a problem.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,251
    Rep Power
    19

    Default Re: Finding where variable is defined and where referenced

    NetBeans has a settable warning for 'Local variable hides a field' and a context menu option 'Navigate --> Go to declaration'

    db

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Finding where variable is defined and where referenced

    Thanks. Now for eclipse.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Finding where variable is defined and where referenced

    They all have switches in the compiler warning options.
    For Eclipse the reference can be found here.
    You want the third block, "Name shadowing and conflicts".
    Fubarable likes this.

Similar Threads

  1. Replies: 1
    Last Post: 02-17-2011, 05:21 PM
  2. Replies: 5
    Last Post: 12-01-2010, 06:43 AM
  3. Replies: 5
    Last Post: 10-15-2010, 04:21 AM
  4. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •