Refactoring

In general, refactoring is a method to improve the design of existing software, without changing its behavior.

In TwinCAT, refactoring offers functionalities for renaming object and variable names and for updating function block connections. You can display all locations of renamed objects and variables show and then rename them individually or collectively. In addition, in the TwinCAT options (Tools > Options) under category TwinCAT > PLC Environment > Refactoring you can configure whether and where TwinCAT automatically issues refactoring prompts.

Renaming a global variable

Renaming a global variable across the project:

You have opened a project containing at least one function block FB_Sample and a global variable list GVL. The global variable list is open in your editor and contains the declaration of a variable, for example nGlob1. FB_Sample uses nGlob1.
1. Select the name of a global variable, for example “nGlob1”.
2. In the context menu select the command Refactoring > Rename 'nGlob1'.
3. In the dialog Rename enter a new name in the input field New name, for example “nGlobNew”, and click OK.
The Refactoring dialog opens. In the project tree on the left the objects GVL and FB_Sample are shown in red with a yellow background. In the window on the right, FB_Sample is open in its editor, and nGlob1 is already renamed to nGlobNew.
4. Click on OK.
In your project all instances of the global variable nGlob1 have been renamed to nGlobNew.

Renaming a global variable across the project with the exception of a POU:

1. Select the name of a global variable, for example “nGlob1”.
2. In the context menu select the command Refactoring > Rename 'nGlob1'.
3. In the dialog Rename enter a new name in the input field New name, for example “nGlobNew”, and click OK.
The Refactoring dialog opens. In the project tree on the left the objects GVL and FB_Sample are shown in red with a yellow background. In the right window, the function block FB_Sample is open in its editor. Instead of nGlob1, nGlobNew is listed.
4. Position the cursor in the window on the right and open the context menu.
5. Select the command Reject this object and click OK.
Your project contains the global variable nGlob1 in FB_Sample. The variable nGlobNew is specified in the objects in which the variable occurred otherwise. The message window shows an error message, indicating that the identifier nGlob1 is not defined.

See also:

Adding and removing input variables

In the declaration part of function blocks for refactoring commands, you can add or remove input or output variables. TwinCAT updates the locations where the function blocks are called/used accordingly; you can accept or discard the update for each location. The Refactoring dialog is used for this purpose.

You have opened a function F_Sample in the editor. The function already has input variables nInput1, nInput2 and nInputx. It is called in the programs MAIN and POU.
1. Put the focus in the declaration part of the function F_Sample.
2. In the context menu select the command Refactoring > Add variable.
The standard dialog for declaring a variable appears.
3. Declare the variable nInput3 with scope VAR_INPUT and data type INT. Close the dialog with OK.
The Refactoring dialog appears (see figure below). The affected areas are marked in yellow.
4. At the top right select the option Add inputs with placeholder text.
5. In the window on the left, place the cursor on one of the objects highlighted in yellow, for example MAIN. In the context menu select the command Accept whole project, in order to add the new variable at the locations where F_Sample is used in the whole project.
The window on the right shows the change in the implementation part of MAIN: Placeholder _REFACTOR_ appears where the new variable was inserted.
6. Close the Refactoring dialog with OK.
Select the command Edit > Find and Replace. Find “_REFACTOR_” in the project, in order to check the affected locations and edit them as required.
Refactoring 1:

Alternatively, you can add the new variable directly with an initialization value of your choice, without using a placeholder to start with. In this case, select the option “Add inputs with the following value” under step 4 and enter the value to the right.

Refactoring 2:

Example for a new variable with placeholder text in a CFC function block:

Refactoring 3:

Please note that refactoring can also be used for removing variables.

See also:

Rearranging variables in the declaration

You can use refactoring to change the order of declarations in the declaration part of function blocks. This is possible for declarations the validity ranges VAR_INPUT, VAR_OUTPUT or VAR_IN_OUT.

VAR_INPUT
    nInVar2 : INT;
    nInVar1 : INT;
    in      : DUT;
    bVar    : BOOL;
    nInVar3 : INT;
END_VAR
You have opened the declaration part of a POU, which may contain the declarations shown above, for example.
1. Place the cursor in this declaration block and press the right mouse button to open the context menu.
2. In the context menu select the command Refactoring > Reorder variables.
The Reorder dialog appears with a list of the VAR_INPUT variables.
3. For example, select the entry nInVar1 : INT; and use the mouse to drag it before the entry nInVar2 : INT;.
The declaration of nInVar1 is now at the top.
4. Close the dialog with OK.
The Refactoring dialog appears. The affected areas are marked in yellow (see figure at the top).
5. Confirm with OK.
The new order is transferred to the function block.

See also:

Changing a variable declaration and applying refactoring automatically

Refactoring provides support for renaming variables in the declaration (using the autodeclaration feature).

You have opened a function block FB_Sample in the editor. The function block has an input variable “nVAR”.
In the TwinCAT options (Tools > Options) in the TwinCAT > PLC Environment > Refactoring category, the option On renaming variables is activated.
1. Select the variable nVar in the declaration of FB_Sample. Alternatively, you can place the cursor before or in the variable.
2. Select the Auto Declare command in the Edit menu or in the context menu.
The Auto Declare dialog opens. The dialog contains the settings of “nVar”.
3. Change the name from “nVar” to “nCounterVar”.
4. The Apply changes using refactoring option appears and is enabled. When changing the variable name, this option appears independently of the settings in the TwinCAT options. However, it is only activated automatically if the TwinCAT refactoring option mentioned in the prerequisites is activated.
5. Click on OK.
The dialog Refactoring opens. All locations affected by the variable renaming are marked there and can be changed.

See also: