Back to TTCN-3 Refactoring Catalog

Merge Template

Replace several template declarations of the same type using different or even the same values for the same fields with one single template.

Motivation

Occasionally, there are several template declarations of the same type which are either completely the same or at least basically similar, because they vary only in values at the same fields. In this case these templates (source template declarations) may be merged into one single target template declaration, either by using the Parameterize Template or Remove Duplicate Template. Such a change removes code duplication, improves maintainability and increases flexibility. If the template declarations are similar, but the values vary in differing fields, the Replace Template with Modified Template refactoring may be a better choice.

This refactoring may be considered as some sort of "meta" refactoring, because it gives basically guidelines when to apply one of the refactorings Parameterize Template and Remove Duplicate Template, but does not provide any actual transformation steps.

Mechanics

  • Check whether all source templates are of the same type and all fields of the source templates are identical: If yes, apply Remove Duplicate Template.
  • Check whether all source templates are of the same type, but differ in the same fields: If yes, apply Parameterize Template.

Example

The following listing shows an unrefactored example. The source template declarations a_firstTemplate and a_secondTemplate are completely the same. Hence, Remove Duplicate Template is applicable.

type record ExampleType {
	boolean ipv6,
	charstring ipAddress
}

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

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

testcase tc_exampleTestCase() runs on ExampleComponent {
	pt.send( a_firstTemplate );
	pt.send( a_secondTemplate );
}

The following listing shows another unrefactored example. The source template declarations a_firstTemplate and a_secondTemplate have the same type and differ only in the values of ipAddress. Hence, Parameterize Template is applicable.

type record ExampleType {
	boolean ipv6,
	charstring ipAddress
}

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

template ExampleType a_secondTemplate := { 
	ipv6		:= false,
	ipAddress	:= "134.72.13.2"
} 

testcase tc_exampleTestCase() runs on ExampleComponent {
	pt.send( a_firstTemplate );
	pt.send( a_secondTemplate );
}

Back to TTCN-3 Refactoring Catalog