Moving Code into Script Libraries

Overview

This guide outlines the process of organizing and reusing code by moving it into script libraries. By placing shared code in script libraries, you can reduce redundancy, improve maintainability, and ensure consistency across multiple modification methods, task stage hooks, and workflows.

Prerequisites

Before following this guide, ensure the following: - Familiarity with script languages used within the XDM environment. - Access to the XDM configuration objects.

Steps to Move Code into Script Libraries

1. Creating a Script Library

A script library is a reusable collection of code placed in a dedicated file within the XDM configuration objects. These libraries allow you to centralize code that is used across multiple locations.

Steps:

  1. Create a new file under the XDM configuration objects.

  2. Assign a name to the file that reflects its functionality.

  3. Choose the appropriate script language that matches the language used in the code you intend to reuse.

Script libraries can only be used in scripts that are written in the same script language.

2. Moving the Shared Code to the Script Library

Once the script library file is created, move the code snippets that are shared across multiple objects (e.g., modification methods, task stage hooks, workflows) into the script library file.

Steps:

  1. Identify the code sections in the target object (modification method, task stage hook, or workflow) that should be reused.

  2. Create functions within the script library that encapsulate these code sections.

  3. Replace the original code with calls to the functions from the script library.

Example:

Original Code in Modification Method:

def apply() {
    data[columnIndex] = data[columnIndex].replace(" ", "");
}

This code removes spaces from a value in dataArray. To move this to a script library, you would wrap the logic in a function.

Code in Script Library File:

def removeSpaces(value) {
    return value.replace(" ", "");
}

Modified Code in the Original Object (e.g., Modification Method):

def apply() {
    data[columnIndex] = removeSpaces(data[columnIndex]);
}

3. Referencing the Script Library in the Original Object

To enable the usage of the shared functions from the script library, reference the script library file in the object from which the code was moved.

Steps:

  1. Open the configuration of the object (modification method, task stage hook, or workflow).

  2. Locate the Libraries section in the configuration.

  3. Add a reference to the script library file by choosing the file name created in Step 1.

4. Testing and Validation

After integrating the script library into your objects, test the functionality to ensure that the shared code is executed correctly in the new context. Validate that there are no errors, and that the modification methods, task stage hooks, or workflows behave as expected with the shared library functions.

Benefits of Using Script Libraries

  • Reusability: Reuse the same code across multiple locations without duplicating it.

  • Maintainability: Manage shared logic in one location, making updates easier and reducing the risk of inconsistencies.

  • Cleaner Code: Keep your modification methods, task stage hooks, and workflows concise and focused on their primary responsibilities.

Conclusion

By moving frequently used code into script libraries, you can centralize logic, improve code maintainability, and reduce redundancy. Script libraries provide a clean and efficient way to manage shared code and simplify maintenance tasks in the XDM environment.