Wing Tips: Introducing Variables with Refactoring in Wing Pro

Aug 26, 2019


In past issues of Wing Tips we covered a number of the refactoring operations available in Wing Pro, such as renaming symbols, moving symbols, and introducing functions and methods. To finish our series on refactoring, let's take a look at how to introduce a variable based on existing Python code, using Wing Pro's Introduce Variable refactoring operation.

This operation is used to replace selected occurrences of an expression with a new local variable, either to make code more readable or to avoid redundant computation.

Example

Here's a simple example that introduces a local variable tokens to replace repeated use of the expression logical.fTokens, in order to make the code more readable:

/images/blog/refactor-intro-var/introduce-example.gif

Shown Above: Select the expression "logical.fTokens" to assign to the new local variable, Right-click to initiate Introduce Variable, type in the variable name "tokens", execute the introduce operation, select the new line of code, and then select the new variable to highlight where it is being used.

Notice that Wing replaces all occurrences of the selected expression logical.fTokens with a reference to the new variable tokens. Using refactoring to introduce a new variable is usually much easier and less prone to errors than making edits of this type manually.

When only a subset of the instances of an expression should be replaced with the new variable, some matches in the Refactoring tool can be unchecked, or in some cases using multi-selection may be preferable.

Try It Yourself

You can easily try this out in your copy of Wing Pro, by selecting any expression in your Python code and choosing Introduce Variable from the Refactor menu. As in the above example, you will be asked to choose the name of the new variable and Wing will replace all occurrences of the expression with a reference to the new variable. An unwanted introduction can be backed out with the Revert button in the Refactoring tool.



That's it for now! We'll be back soon with more Wing Tips for Wing Python IDE.



Share this article: