Back to TTCN-3 Refactoring Catalog

Decompose Template

Decompose complex template declarations into smaller templates using references.

Motivation

Template declarations for complex types can easily be very long. They become hard to read and maintain. In addition to this, the longer a single template declaration is, the more likely that it is highly specialized and cannot be reused in other places. It may be possible though to reuse single parts of the complex template. A template may be decomposed into smaller templates in TTCN-3. The originally complex template then references the small subtemplates instead of defining every value within itself. Application of this refactoring should not be exaggerated. Too many subtemplates or too many small templates are confusing as well. Refactorings that may be used after decomposing a template are Parameterize Template or Replace Template with Modified Template.

Mechanics

  • Create a new target template for each field of the source template which contains a record or list type. The type of the target template is determined by the type of the corresponding field of the source template. The name usually relates to the name of the corresponding field of the source template.
  • Copy each value specification from the source template into its corresponding smaller target template.
  • Compile.
  • Repeat the following steps until all record or list type fields of the source template are replaced with references to the corresponding target template:
    • Replace one value specification from the source template with a reference to its corresponding target template.
    • Compile and validate.

Example

The example is derived from an ETSI SIP test suite. The next listing shows the rather complex template a_contactAddress which specifies all fields right in the template declaration.

type record ContactAddress
{
  Addr_Union 		addressField,
  SemicolonParam_List	contactParams optional
}

template ContactAddress a_contactAddress := { 
	addressField := {
		nameAddr := {
			displayName := "ETSI Tester",
			addrSpec := omit
		}
	},
	contactParams := {{
		id := "transport", 
		paramValue := PX_TRANSPORT
	}}
} 

The following listing shows the refactored version. The values of addressField and contactParams have been moved to their own templates and are then referenced within the originally complex template.

type record ContactAddress
{
  Addr_Union 		addressField,
  SemicolonParam_List	contactParams optional
}

template Addr_Union a_specificAddressField := { 
	nameAddr := {
		displayName := "ETSI Tester",
		addrSpec := omit
	}
} 

template SemicolonParam_List a_specificContactParams := { 
	{
		id := "transport", 
		paramValue := PX_TRANSPORT
	}
} 

template ContactAddress a_contactAddress := { 
	addressField := a_specificAddressField,
	contactParams := a_specificContactParams
} 

Back to TTCN-3 Refactoring Catalog