Productivity hints: Writing assignments with ‘Introduce Variable’ refactoring

Hello guys,

Recently after demoing the PhpStorm to users on a conference I discovered that our practice of writing assignments “backwards” via “Introduce variable” was really surprising to users… Lets discuss what exactly we are doing and why.

How do you normally write assignment?
First of all, you write the name of the variable, then comes the equals sign ‘=’, and then the right-hand side of the statement. While writing the right-hand side of a statement you often use completion, so at the beginning you do not know what exactly the right-hand side will look like. Nevertheless, you need to find a name for the variable before you have completed the right-hand side of the assignment.

Suppose, you want to write the following assignment statement:

Let’s start writing it from its right-hand side. Type ‘$this->’:

Select the required method from the list:

With the cursor at the end of the line, invoke the ‘Introduce Variable’ refactoring (Ctrl+Alt+V for WindowsLinux and Cmd+Opt+V for Mac).
The IDE tries to suggest a name for the variable based on its type or the called function, etc. Rather often, the suggested name is the one you would assign the variable yourself.

Click OK.

Develop with pleasure!

This blog is permanently closed.

For up-to-date information please follow to corresponding WebStorm blog or PhpStorm blog.

 
This entry was posted in Cool Feature, PhpStorm and tagged , , , . Bookmark the permalink.

7 Responses to Productivity hints: Writing assignments with ‘Introduce Variable’ refactoring

  1. Bryan Green says:

    As always, me rikey.

    -Bryan

  2. Johnny Woo says:

    This is awesome! It definitely should be one of those productivity tips, if it’s not there yet.

  3. Vladimir says:

    It’s work only inside class methods. But if you try

    $dec=new DecoratorFactory(); //for example
    $dec->getDecorators()

    then Ctrl+Alt+V (Win), etc…

    you receive other result.

  4. alinares says:

    It is a great feature, but it would be better if the dialog didn’t popped up. Just leave the cursor ready to write the variable between the ‘$’ and ‘=’ characters, and when hitting tab move it to where it originally was :)

  5. Nikolay Matveev says:

    @Vladimir
    Can’t reproduce. Please provide screenshot(you can create a new issue in our issue tracker and attach screenshot there).

    @alinares
    Please create a new feature request – http://youtrack.jetbrains.net/issues/WI#newissue=yes

  6. Shaun says:

    I don’t use this feature that often, but it definitely saved me some time today when I did need it.

  7. Nikolay Matveev says:

    @Shaun
    Thanks for feedback!

Comments are closed.