Back to TTCN-3 Refactoring Catalog

Inline Template Parameter

A formal parameter of a template which is always given the same actual value is inlined.

Motivation

Templates are typically parameterized to avoid multiple template declarations that differ only in few values. However, as test suites grow and change over time, the usage of its templates may change as well. As a result, there may be situations when all references to a parameterized template have one or more actual parameters with the same values. This can also happen when the test engineer is overly eager: a template is parametrised as it is believed it might be useful for future use, but it later turns out to be unnecessary. In any case, there are template references with unneeded parameters creating code clutter and more complexity than useful. Thus, the template parameter should be inlined and removed from all references.

The concept of inlining parameterized templates is used within this refactoring. Therefore, it is partially related to the Inline Template refactoring.

Mechanics

  • Verify that all template references to the parameterized source template declaration have a common actual parameter value. The parameter with the common actual parameter value is the source parameter. Record the common value.
    • If more than one formal parameter is always referenced with a common actual parameter value, it is easier to inline them together. Therefore, perform each step that concerns the source parameter for all concerned parameters at once.
  • Copy the source template declaration within the same scope and group. Give the copied declaration a temporary name. This is the target template declaration.
  • In the target template declaration body, replace each reference to the source formal parameter with the noted value from the first step. In the target template declaration signature, remove the parameter corresponding to the source parameter.
  • Compile.
  • Remove the source template declaration.
  • Rename the name of the target template declaration using the name of the source template declaration.
  • Find all references to the target template declaration. Remove the source parameter from the actual parameter list of each reference.
  • Compile and validate.
  • Consider usage of the Rename refactoring to improve the target template declaration name.

Example

The next listing contains the parameterized template exampleTemplate. All references to this template use the same actual parameter value. Hence, the corresponding parameter addressParameter is inlined.

type record ExampleType {
	boolean ipv6,
	charstring ipAddress
}

template ExampleType exampleTemplate( charstring addressParameter ) := {
	ipv6 := false,
	ipAddress := addressParameter
}

testcase exampleTestCase() runs on ExampleComponent {
	pt.send( exampleTemplate( "127.0.0.1" ) );
	pt.receive( exampleTemplate( "127.0.0.1" ) );
}

After applying the Inline Template Parameter refactoring (next listing), the string value "127.0.0.1" is inlined into the template body of exampleTemplate, the corresponding formal parameter of the template and the corresponding actual parameter of each reference to exampleTemplate are removed.

type record ExampleType {
	boolean ipv6,
	charstring ipAddress
}

template ExampleType exampleTemplate := {
	ipv6 := false,
	ipAddress :=  "127.0.0.1"
}

testcase exampleTestCase() runs on ExampleComponent {
	pt.send( exampleTemplate );
	pt.receive( exampleTemplate );
}

Back to TTCN-3 Refactoring Catalog