Refactoring

In general, refactoring is a technique for improving the design of existing software code without changing the way it functions.

In CODESYS, refactoring provides functions for renaming objects and variables and updating referenced pins. You can display all occurrences of renamed objects and variables and then rename them all at once or individually. In Tools ‣ Options , you can also configure where CODESYS will prompt you for refactoring.

Renaming global variables

Requirement: A project is open that includes at least a function block FB and a global variable list. The global variable list GVL is open in the editor and contains a variable declaration (example: iGlobal). FB uses iGlobal.

Renaming global variables throughout the project
  1. Select the global variable name iGlobal.

  2. Right-click the variable and click Refactoring ‣ Rename iGlobal .

  3. In the Rename dialog, type a name in the New name input field, for example iGlobalOK, and click OK.

    ⇒ The Refactoring dialog opens. In the device tree view on the left, the GVL and FB objects are highlighted in red and yellow. In the view on the right, FB in is open in its editor and iGlobal has already been renamed as iGlobalOK.

  4. Click OK.

  5. ⇒ No global variable iGlobal is in your project. Now iGlobalOK is everywhere.

Renaming global variables throughout the project (except for a POU)
  1. Select the global variable name iGlobal.

  2. Right-click the variable and click Refactoring ‣ Rename iGlobal .

  3. In the Rename dialog, type a name in the New name input field, for example iGlobalTest, and click OK.

    ⇒ The Refactoring dialog opens. In the device tree view on the left, the GVL and FB objects are highlighted in red and yellow. In the window on the right, the function block FB is open in its editor. iGlobalTest is listed instead of iGlobal.

  4. Right-click in the view on the right.

  5. Click Reject this object and click OK.

    ⇒ The global variable iGlobal is available in FB in your project. The variable iGlobalTest is now specified in the objects where the previous variable occurred.

    The error message in the message view reports that the iGlobal identifier is not defined.

Adding and removing input variables

In the declaration part of blocks, you can add and delete input and output variables by using the refactoring commands. CODESYS performs updates at the occurrence locations and calling locations of the blocks. You can accept or reject these updates individually. The Refactoring dialog also opens for this purpose.

Requirement: The FCT (function type) POU is open in the editor. The function already contains the input variables input1, input2, and inputx. They are called in the PLC_PRG and POU programs.

  1. Set the focus in the declaration part of the FCT function.

  2. Click Refactoring ‣ Add Variable .

    ⇒ The default dialog opens for declaring variables.

  3. Declare the variable input_3 with the validity range of VAR_INPUT and data type INT. Click OK to close the dialog.

    ⇒ The Refactoring dialog opens (see figure below). The affected locations are marked in yellow. (1)+(2)

  4. In the upper right corner, select Add inputs with placeholder text from the drop-down list. (3).

  5. In the left side of the window, click one of the highlighted objects (for example, PLC_PRG). Right-click and choose the Accept whole project command to add the new variable at the new location of use in FCT for the entire project.

    ⇒ You see the change in the implementation part of PLC_PRG in the view on the right: The placeholder _REFACTOR_ appears at the location where the new variable was added.

  6. Click OK to close the Refactoring dialog.

  7. Click Edit ‣ Find . Search the project for “_REFACTOR_” to check and edit the affected locations.

  8. Note: As an alternative, you can insert the new variable with another initialization value without working with a placeholder first. In this case, in Step 4 you select “Add inputs with the following value” and type the value in the field on the right side of the drop-down list.

Example of a new variable with placeholder text in a CFC block:

Please note that you can also remove variables with refactoring.

Reordering variables in the declaration

In the declaration part of function blocks, you can change the order of declarations by refactoring This is possible for declarations with validity range VAR_INPUT, VAR_OUTPUT, or VAR_IN_OUT.

Requirement: The declaration part of a POU is open and includes declarations, for example:

VAR_INPUT
  invar2 : INT;
  invar1 : INT;
  in : DUT;
  bvar : BOOL;
  invar3 : INT;
END_VAR
  1. Right-click in this declaration block to access the context menu.

  2. Click Refactoring ‣ Reorder variables .

    ⇒ The Reorder dialog opens with a list of VAR_INPUT variables.

  3. Drag the invar1 : INT; entry to the position before the invar2. entry.

    ⇒ The invar1 declaration is now at the top position.

  4. Click OK to close the dialog.

    ⇒ The Refactoring dialog opens. The affected locations are marked in yellow (see figure above).

  5. Click OK to accept the new order for the function block.

Changing a variable declaration and applying refactoring automatically

Refactoring helps you in the declaration when renaming variables (by means of “Auto declare”).

Requirement: Function block fb_A.

  1. Click Tools ‣ Options .

    ⇒ The Options dialog opens.

  2. Select the Refactoring category.

  3. In Auto declare, activate the options On renaming variables and On adding or removing variables, or for changing the namespace.

  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 set the cursor before or in the variable.

  6. Specify Edit ‣ Declare variable (Shift+F2).

    ⇒ The Declare variable dialog opens. The dialog includes the settings of iA.

  7. Change the name of iA to iCounter_A.

  8. The option Changes by means of refactoring appears and is activated.

  9. Click OK.

    ⇒ The dialog Refactoring Renaming from iA to iCounterA opens. All locations affected by the variable renaming are marked there.

  10. Click OK to close the dialog.

    ⇒ The changes are applied.

See also