Generally refactoring is the process of restructuring and improving existing software code without changing its behavior.
Refactoring in EcoStruxure Machine Expert provides functionalities for renaming object names and variable names. It allows you to show all usage locations of changed objects and variables and to get renamed all those at once, or only individual ones. You can configure whether and at which locations you should get asked for performing refactoring. Explicitly you can use the command Refactoring > Rename.
For further information, also refer to Options, Refactoring.
The command Refactoring > Rename is used for a global, project wide renaming of object names, and variable names.
It is available in the context menu if you set the cursor on a variable name in the declaration or if you right-click one of the following objects in a navigator:
oFunctions
oPOUs
oGVLs
oMethods
oProperties
oDevices
The command is also available if you select variables and unit conversions within the editor for unit conversions.
The command is not available in online mode.
After you have executed the Refactoring > Rename command, a dialog is displayed. It shows the locations where the concerned name is used: In the right part of the window the particular locations, in the left part the navigation tree where the concerned object is marked.
You can select the following commands for each location individually:
oReject/accept this change: Reject/accept single changes in the right window.
oReject/accept this object: Reject/accept all changes in the concerned object.
oReject/accept whole project: Reject/accept all changes in the project.
Accepted changes are displayed with a yellow background, rejected changes with gray background.
The command Refactoring > Add <variable> allows you to declare variables in a POU and performs an automatic update at its different points of use.
It is available in the context menu or in the Edit > Refactoring menu if the cursor is placed in a declaration editor. The Auto Declare dialog box opens.
After you have confirmed the Auto Declare dialog box by clicking OK, the Refactoring dialog box opens. It consists of the tree structure of the project on the left-hand side. The POUs where the new variable is used are highlighted in the tree. You can double-click a POU to open the detailed view on the right-hand side of the dialog box. In this declaration section and implementation of the POU on the right-hand side, new declarations are highlighted in yellow.
Before you decide which changes you want to accept at which location, select the suitable option from the list above the declaration section and implementation of the POU on the right-hand side:
Option |
Description |
---|---|
Add inputs with placeholder text |
Select this option to insert the standard placeholder text _REFACTOR_ whenever the new variable occurs in the implementation code. You can search for this standard text later in order to identify the affected locations. |
Add inputs with the following value |
Select this option to insert an initialization value whenever the new variable occurs in the implementation code. Enter the initialization value in the text box on the right-hand side of the option Add inputs with the following value. |
In order to accept or reject changes, right-click the changed locations or execute the commands in the left or right part of the dialog box, as described in the Process of Renaming paragraph in this chapter.
Examples for Adding a Variable
Example 1:
By refactoring, the fun block receives a new input variable input3 with the initialization value 1. The change has the following effect:
Before refactoring:
fun(a + b, 3, TRUE);
fun(input1:= a + b , input2 :=3 , inputx := TRUE);
After refactoring:
fun(a + b, 3, input3:=1, TRUE);
fun(input1:= a + b , input2 :=3 , input3:=_REFACTOR_,inputx := TRUE);
Example 2:
By refactoring, the fun block receives a new input variable input3 with the placeholder text _REFACTOR_:
Before refactoring:
inst(input1 := a + b, input2 := 3, inputx := TRUE);
fun(a + b, 3, TRUE);
After refactoring:
inst(input1 := a + b, input2 := 3, input3 := _REFACTOR_, inputx := TRUE);
fun(a + b, 3, input3:=_REFACTOR_, TRUE);
The command Refactoring > Remove <variable> allows you to remove an input or output variable from a POU. It is removed automatically from the POU at its different points of use.
The command is available in the context menu or in the Edit > Refactoring menu if the cursor is placed in the declaration editor on the identifier of the variable to be removed. A dialog box opens that provides information on the removal. If you confirm the message, the Refactoring dialog box opens.
When you accept the changes proposed in the Refactoring dialog box, the respective input or output variables are removed automatically from the respective POU at its different points of use.
NOTE: In CFC, only the connection is removed between the removed input or output and the block. The input or output itself remains in the chart.
Example for Removing a Variable in ST
In a POU, refactoring removes the input4 input variable. It is automatically removed from the respective POU at its different points of use:
Before removal:
inst(input1 := a + b, input2 := 3, input4 := 1, input5 := TRUE);
fun(a + b, 3, 1, TRUE);
After removal:
inst(input1 := a + b, input2 := 3, input5 := TRUE);
fun(a + b, 3, TRUE);
Command Update Referenced Pins
The command Refactoring > Update Referenced Pins only takes effect on the CFC editor and the FBD/LD/IL editor. It corresponds to the commands Reset Pins and Update parameters.
The command Refactoring > Update Referenced Pins updates the input and output pins of the POU, at its different points of use, according to the current POU declaration.
It is available in the context menu or in the Edit > Refactoring menu if the cursor is placed in the POU name, in the first line of the POU declaration or in the device editor.
The command Refactoring > Reorder Variables allows you to change the order of variable declarations within the scopes VAR_INPUT, VAR_OUTPUT, or VAR_IN_OUT.
Example:
VAR_INPUT
invar2 : INT;
invar1 : INT;
in : DUT;
bvar : BOOL;
invar3 : INT;
Proceed as follows to change the order of declarations:
Step |
Action |
---|---|
1 |
Right-click the declaration part and execute the command Refactoring > Reorder Variables. Result: The Reorder dialog box opens with a list of VAR_INPUT variables. |
2 |
Drag the invar1 : INT; entry to the position before the invar2. entry. Result: The invar1 declaration is at the top position. |
3 |
Click OK to close the dialog box. Result: The Refactoring dialog box opens. The affected elements are marked in yellow. |
4 |
Click OK to accept the new order of parameters of the function block. Have in mind that the modified order of the parameters will be reflected also at the call usages of the POUs. |
Modifying a Variable Declaration and Applying Refactoring Automatically
Refactoring can help you in renaming variables (using Auto Declare) as indicated in the following example.
As a prerequisite for this example, a function block fb_A with an input iA must be available.
Step |
Action |
Comment |
---|---|---|
1 |
Execute the command Tools > Options.... |
Result: The Options dialog box opens. |
2 |
Select the category Refactoring on the left-hand side. |
– |
3 |
In the Auto Declare section, select the options On adding or removing variables, or on changing the scope and On renaming variables. |
– |
4 |
Double-click the function block fb_A. |
– |
5 |
Select a variable in the declaration of fb_A, for example, iA. |
As an alternative, you can place the cursor before or in the variable. |
6 |
Execute the command Edit > Auto Declare.... |
As an alternative, use the shortcut Shift + F2. Result: The Auto Declare dialog box opens. The settings of the variable iA are displayed. |
7 |
Edit the Name and change iA into iCounter_A. |
Result: The Refactoring - Renaming from iA to iCounterA dialog box opens. The POUs where the variable is used are highlighted in the tree structure. |
8 |
Click OK to apply the renaming and to close the dialog box. |
– |