Back to TTCN-3 Refactoring Catalog

Move Component Variable/Constant/Timer to Local Scope

A component variable, constant or timer is moved to a local scope when only used in a single function, altstep or test case.

Motivation

When a local variable, constant or timer declaration of a component is referenced only by a single function, altstep or test case, the reusability of this function, altstep or test case can be improved when the component declaration is moved to the local scope of the function, altstep or test case. This way, its coupling to the component is reduced or removed. As a result, the runs on clause can possibly be generalized with the Generalize Runs On refactoring to allow its use on other components or it may even become superfluous when no other component declaration is referenced. Hence, component declarations should always be used by multiple functions, altsteps or test cases.

Mechanics

  • Inspect all functions, altsteps and test cases running on the source component to ensure the target function, altstep or test case is indeed the only one referencing the concerned declaration in the source component.
  • Copy the declaration (including a possible initialization value) from the source component to the target function, altstep or test case. The copied declaration should be the first statement within the target.
  • Compile
  • Remove the declaration from the source component.
  • Compile and validate.
  • Consider the application on Generalize Runs On or remove the runs on clause when no more declarations from the source component are referenced within the target.

Example

The component MyComponent? contains a local declaration myInt which is used in the function f_myFunction.

type component MyComponent { 
	// ...
	var integer myInt; 
} 

function f_myFunction() runs on MyComponent { 
	myInt := 255; 
} 

As f_myFunction is the only function, altstep or test case referencing this component declaration, it is moved as local declaration to the function itself. As a result, f_myFunction no longer depends on MyComponent? as no ports or other declaration local to the component are used. Therefore, the runs on clause is removed.

type component MyComponent {
	// ...
}

function f_myFunction() { 
	var integer myInt; 
	myInt := 255; 
} 

Back to TTCN-3 Refactoring Catalog