Back to TTCN-3 Refactoring Catalog
Remove Duplicate Template
Replace several template declarations which have the same body with one single template.
Motivation
Occasionally, there are several template declarations which are duplicate, i.e. they are of the same type and have in all fields the same values. Instead of keeping the duplicate template declarations (source template declarations), they are replaced with one single target template declaration. Such a change removes code duplication, improves maintainability and increases flexibility. If the names of the source templates convey important information which would get lost by using a single template, their names may be kept by delegating them to the target template. If not all template fields are completely the same, but differ in the same fields: use the Parameterize Template refactoring. 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.
Mechanics
- Copy one of the source template declarations (signature and body) and give this target template a name which reflects the meaning of all different usages of this template.
- Compile.
- Now, decide whether you like to keep the source template names for delegation (because their names convey important information which would get lost when using only the target template) or whether you like to keep only the single target template.
- If like to remove the source templates completely and keep only the target template:
- Repeat the following steps for all references to the source template declarations:
- Replace the reference with a reference to the target template.
- Compile and validate.
- Remove the source template declarations from the code. They should not be referenced anymore.
- Compile and validate.
- Repeat the following steps for all references to the source template declarations:
- If like to keep the source templates and want just delegate to the target template:
- Repeat the following steps for all source template declarations:
- Replace the template body with a reference to the target template.
- Compile and validate.
- Repeat the following steps for all source template declarations:
- If like to remove the source templates completely and keep only the target template:
Example
The following listing shows the unrefactored example. The source template declarations a_firstTemplate and a_secondTemplate have the same body.
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 resulting code after applying Remove Duplicate Template using the variant to remove all duplicate template declarations completely is shown below. One copy of the duplicate templates has been given a more appropriate name (a_singleTemplate) and all remaining duplicate template declarations hab been removed. The references to a_firstTemplate and a_secondTemplate are replaced with a_singleTemplate.
type record ExampleType {
boolean ipv6,
charstring ipAddress
}
template ExampleType a_singleTemplate := {
ipv6 := false,
ipAddress := "127.0.0.1"
}
testcase tc_exampleTestCase() runs on ExampleComponent {
pt.send( a_singleTemplate );
pt.send( a_singleTemplate );
}
The resulting code after applying Remove Duplicate Template using the variant of keeping all source templates and delegating to the new parameterized target template is shown below. A new target template declaration a_singleTemplate is created which has the body of the duplicate templates. The bodies of a_firstTemplate and a_secondTemplate are replaced with referefences to a_singleTemplate.
type record ExampleType {
boolean ipv6,
charstring ipAddress
}
template ExampleType a_singleTemplate := {
ipv6 := false,
ipAddress := "127.0.0.1"
}
template ExampleType a_firstTemplate := a_singleTemplate
template ExampleType a_secondTemplate := a_singleTemplate
testcase tc_exampleTestCase() runs on ExampleComponent {
pt.send( a_firstTemplate );
pt.send( a_secondTemplate );
}
