Back to TTCN-3 Refactoring Catalog

Replace Template with Modified Template

Templates of structured or list types of the same type with similar content values that differ in few different fields can be simplified by using modified templates.

Motivation

When several templates of the same structured or list types have mostly the same content values and differ only in few fields, these templates (targets) can be simplified by using modified templates. A modified template declaration uses another template declaration as base (source), but redefines only the fields where it differs. All other values are inherited from the base template.

While this may sound similar to what the Parameterize Template refactoring does, the difference is that a parameterized template can combine only templates where one or more of the same fields differ (a template parameter is referenced in those fields) while a modified template simplifies the declaration of templates where the values are different mostly in varying fields. Replacing a target template declaration with a modified template improves maintenance and reusability as value changes in the source base template can be made at a single location (i.e. the source base template).

Mechanics

  • Find template declarations of the same structured or list types that differ only in few values.
  • Choose the source base template declaration from the template declarations found in the previous step. The source base template declaration is the template declaration which has the most non-differing values if compared with all other remaining templates. All other template declarations found in the previous step are the target templates.
  • For each target template, repeat the following steps:
    • Modify the target template declaration to be a modified template of the source base template, i.e. remove all duplicate value entries and add a modifies specification.
    • Compile and validate.

//TODO: handling of parameterised templates is unclear!

Example

In the unrefactored example, three templates with varying values are declared which are very similar. After careful inspection, the template a_firstTemplate is chosen as the source base template as it is necessary to redefine only one single value for each target template.

type record ExampleType {
	boolean ipv6,
	integer examplePort,
	charstring ipAddress,
	charstring id
}
template ExampleType a_firstTemplate := { 
	ipv6		:= false,
	examplePort	:= 80,
	ipAddress	:= "127.0.0.1",
	id		:= 1
} 
template ExampleType a_secondTemplate := { 
	ipv6		:= false,
	examplePort	:= 80,
	ipAddress	:= "134.72.13.2",
	id		:= 1
} 
template ExampleType a_thirdTemplate := { 
	ipv6		:= false,
	examplePort	:= 80,
	ipAddress	:= "127.0.0.1",
	id		:= 2
} 

After applying the refactoring Replace Template with Modified Template, the target templates a_secondTemplate and a_thirdTemplate are now declared with a_firstTemplate as base template using the modifies keyword. The common values from a_secondTemplate and a_thirdTemplate are removed.

template ExampleType a_firstTemplate := { 
	ipv6		:= false,
	examplePort	:= 80,
	ipAddress	:= "127.0.0.1",
	id		:= 1
} 
template ExampleType a_secondTemplate modifies a_firstTemplate := { 
	ipAddress	:= "134.72.13.2"
} 
template ExampleType a_thirdTemplate modifies a_firstTemplate := { 
	id		:= 2
} 

Back to TTCN-3 Refactoring Catalog