Refactoring

Refactoring is technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.

You may want to modify existing workflow steps to reflect changing business needs in due course. For example, you may want to:

  1. Rename a workflow step so that it reflects its business intent better.
  2. Add or remove parameters to workflows.
Refactoring allows you to perform these operations without breaking existing tests. If a workflow step is used in more than one scenario, refactoring the workflow step in one scenario affects all the scenarios that use the workflow.

Refactoring is a powerful concept that helps you maintain your tests suites better.

Rephrase refactoring

Rephrase refactoring allows you to rename workflows and change the parameters that it accepts. Consider the Workflow below:

Transfer
  • transfer "1000" from account "1234" to "2345"
  • destination account is credited

You may want to rephrase the workflow step destination account is credited to one of the following:
  1. Check destination account is credited to make it more readable.
  2. Check destination account "2345" is credited "1000" so that you can reuse the workflow step in other scenarios as it now accepts parameters.

Extract Concept

"Extract Concept" makes it possible to merge two or more related statements to create a more meaningful abstraction. For example,
Create Order
  • Create an order "O" as price "23" quantity "50"
  • Submit order "O"
You may wish to refactor the two workflow steps as one logical step:
Create Order
  • Submit an order "O" as price "23" quantity "50"

Inferring parameters in extracted code

Twist allows you to type in parameters in the extracted concept. You could specify existing or new parameters.
  • If you specify a new parameter, Twist introduces a new parameter in your extracted concept
  • If you specify a parameter that is present in any of the extracted workflow steps, Twist parameterizes all occurrences of that parameter in the underlying code. For example,
    Create Order
    • Create an order "O" as price "23" quantity "50"
    • Submit order "O"
    If you extract a concept as
  • Submit an order "O" as price "23" quantity "50"
  • then the underlying code is refactored as follows:
    public void submitAnOrderAsPriceQuantity(String string1, int price, int quantity) {
      createAnOrderAsPriceQuantity(string1, price, quantity);
      submitOrder(string1);
    }