Smarter and Faster Introduce Variable in Java

Introduce variable is one of those refactorings used many times a day. So, how can we make it work faster?
Get rid of modal dialogs!

So, in order to introduce a variable from an expression press Ctrl+Alt+V.

Remember modal dialog? No more of it! Now you can edit your code right in the editor:

Oh, you prefer List as variable type? No problem, press Shift+Tab, choose List from drop down and IntelliJ IDEA will remember your choice for future.

You already pressed Ctrl+Alt+V but then realized that such variable already exists? Not a problem! Now it is possible to reassign a variable right from introduce template. Press Ctrl+Alt+V one more time and IntelliJ IDEA will collect all available variables of appropriate types and will suggest you to choose one:

You can try it yourself using a latest IntelliJ IDEA X EAP.

And let us know what you think!

This entry was posted in New Features and tagged , . Bookmark the permalink.

22 Responses to Smarter and Faster Introduce Variable in Java

  1. Denis Tulskiy says:

    Looks great! Just one thing (maybe you’ve already fixed it) when I press Ctrl-Alt-V and want to accept the name you suggest, I mechanically press Enter and the name is deleted :( Should I make myself to press Tab?

  2. Anna Kozlova says:

    Denis, should be fixed in 98.311. BTW, please watch http://youtrack.jetbrains.net/issue/IDEA-61016. Thank you

  3. Alex says:

    Hi guys! How to customize colors in the dropdown lists (e.g. autocomplete with Ctrl + Space)? Now in 10 version it’s possible?

  4. @Anna Kozlova: Great, thank you. BTW, it says I don’t have access rights to that issue.

  5. Jon says:

    The ‘reassign variable’ action isn’t working here with build 98.382 on Mac OS X 10.6.5.

    I tried with exactly the same code as in the example above. Invoking with ‘introduce variable’ intention works correctly, and suggests pressing the same shortcut again to reassign the variable. But re-invoking the shortcut has no effect (it doesn’t show the ‘title’ and ‘name’ symbols as in your example).

    Should I open a bug report?

  6. Anna Kozlova says:

    @Jon
    Fixed. Thank you

  7. Ara Minosian says:

    This is most frequent refactoring that I use. Thank you.

  8. Jon says:

    @Anna
    Great, thanks!

  9. Chris says:

    I love the fact that it remembers the variable type, but it doesn’t seem to remember the removal of the “Final” designation. I rarely use “Final” variable types and have to delete it every time I introduce a variable

  10. Anna Kozlova says:

    Chris, that was broken in 10.0.1 but is already fixed in 10.0.2 EAP. Sorry for the inconvenience.

  11. scott says:

    I’m using 10.0.3 and still have the final come up. Is there a setting that needs changed somewhere?

  12. Anna Kozlova says:

    You can shift-tab, type chooser will be opened; shift-tab and you’ll be able to choose not to create finals there. IDEA 10.5 provides a different UI to configure these finals though. You may see the sample how it would look like in this blog (Post about Introduce Parameter). Thank you

  13. scott says:

    Worked like a charm! Thanks!

  14. Bijoy Thomas says:

    It would be nice if the introduce variable defaulted to an interface type instead of the concrete class. For e.g., introducing a variable for new HashMap() should default to Map as the type of the introduced variable instead of HashMap. If there are multiple interfaces to choose from, just pick one and have the user hit Shift-Tab to pick the right type.

  15. anet says:

    The statistic is used to choose the the type. So if you choose Map then Map would be suggested. Suggesting interface could be overcomplicated as most of users don’t really care about the type of the local variable and the additional step would bother them rather than get any profit. Thanks for the suggestion

    • Bijoy Thomas says:

      The type of the local variable does matter. For, e.g., if the type introduced is HashMap instead of map and now I use this variable in some undefined function and then use the create method intention, the method created would use HashMap instead of Map in the type of the argument. This is not a good idea because if I change the type of the variable to TreeMap, I now have to change the method as well. I would rather code to interfaces than implementations. Just a suggestion.

      • anet says:

        You’ll get Map after you once choose it.

        I agree that accurately chosen interfaces are better than implementations but we have to find a solution which won’t bother you in most cases. What about Collection in your example? As local variable type it is not always acceptable, you would rather choose List or Map and a chooser is very distructing.

        • Bijoy Thomas says:

          So, the default type should be some interface and the user can then use Shift-Tab to pick the right one.

          So if I did new LinkedList(), the type of the introduced variable should default to an interface (say Collection or List) and I can use Shift-Tab to then pick which one I want.

          I can do that even today, by using Shift-Tab and choosing Map instead of HashMap as the type.

          It would be great if IntelliJ could somehow use smart intentions and know that for commonly used Collections, default to List, Set, Map as the introduced type.

          For user defined types, default to an interface that the class implements and then the user can use Shift-Tab to pick an alternate type.

  16. Bijoy Thomas says:

    I have configured Intellij to highlight unnecessary use of the “this” qualifier. However, I don’t see an action to automatically remove those qualifiers (similar to optimize imports). Is there someway to do this?

  17. anet says:

    There is a quick fix for the inspection which does this optimization. It could be reached on Alt|Enter in the editor (with ability to apply for the whole file in right arrow menu) or on Apply quick fixes in the inspection’s view (scope depends on the scope where you run your inspections).

  18. Bijoy Thomas says:

    Awesome! Thank you. What a time saver!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>