Back to TTCN-3 Refactoring Catalog

Move Local Variables/Constants/Timer to Component

Local declarations of variables, constants and timers can be moved to a component when used in different functions, test cases or altsteps running on the same component to reduce code clutter.

Motivation

When a local variable, constant or timer in a function, test case or altstep that runs on a component is used in more than one function, test case or altstep running on the same component by parameter passing, it might be a good idea to move it to the component. This way, accessibility of this variable and its value is improved within the component scope. The test engineer avoids unnecessary passing of parameters and therefore avoids code clutter. Note that usage of this refactoring should always be used with care as moving variables to the component level can introduce problems similar to global variables depending on the complexity of the component and the number of component variables. Using component variables within functions and test cases fortifies its tie to the component. Hence, detaching such a function, test case or altstep from a component using component variables can be hard.

Mechanics

  • Find the declaration of the local source variable/constant/timer in its function, altstep or test case.
  • Copy this local source variable/constant/timer declaration to the target component as component declaration.
  • If a source variable is initialized with a value at its local declaration, choose whether the initialization must be moved to the target component declaration as well or whether it should be converted into an assignment at the location of the source declaration. This depends on the semantics of the component behavior.
  • Compile.
  • Remove the local source declaration from the function, altstep or test case.
  • Compile and validate.
  • Find functions, altsteps and test cases running on the same target component and called from the scope or subscope of the source declaration where a reference to the source declaration was passed as parameter. For each of these functions, altstep or test cases, remove the corresponding parameter and adjust its body to use references to the target component variable, constant or timer instead.
  • Compile and validate.

Example

In this example, there are two functions f_exampleFunction and f_anotherExampleFunction. A local variable v_exampleVariable is declared in f_exampleFunction and then assigned the value 16. This variable passed as an in parameter to the function f_anotherExampleFunction where it is used in a conditional statement.

// ...
type component ExampleComponent {
	// ...
}

function f_exampleFunction() runs on ExampleComponent { 
	// ...
	var integer v_exampleVariable := 16; 
	// ...
	f_anotherExampleFunction(v_exampleVariable); 
}
function f_anotherExampleFunction(in integer p_exampleParam) 
	runs on ExampleComponent { 
	// ...
	if (p_exampleParam > 0) { 
		// ...
	}
}

// ...

The refactored version moved the local variable declaration including its initialization to the component and adjusts the signature and body of the function f_anotherExampleFunction to use the component variable instead of a parameter.

// ...
type component ExampleComponent {
	// ...
	var integer v_exampleVariable := 16; 
}

function f_exampleFunction() runs on ExampleComponent {
	// ...
	f_anotherExampleFunction();
}

function f_anotherExampleFunction() runs on ExampleComponent { 
	// ...
	if (v_exampleVariable > 0) {
		// ...
	}
}
// ...

Back to TTCN-3 Refactoring Catalog