Back to TTCN-3 Refactoring Catalog

Move Module Constants to Component

When the declaration of a constant at module level is used exclusively by a module component or functions/altsteps/test cases running on this component, it should be moved into the component declaration.

Motivation

Constant declarations at module level can introduce problems when constants with the same name from other modules are imported. This is especially the case when unspecifically all declarations from a module are imported. The programmer may not be aware of the name clash and in the worst case, his IDE or compiler does not provide useful error messages which makes this implicit mistake hard to find. So the constant declarations that are used only within a single component anyway should always be moved into the component declaration to limit its scope. Another method to avoid these kinds of problems is the Prefix Imported Declarations refactoring. When constants are moved to the component, their tie to the component is fortified. Therefore, the decision whether to use Prefix Imported Declarations or Move Module Constants to Component when name clashes happen, depends on the overall context.

Mechanics

  • Verify that the declaration of the selected constant is used exclusively by a module component or functions/altsteps/test cases running on this component
  • Copy the source constant declaration to the target component.
  • Compile.
  • Remove the source constant declaration.
  • Compile and validate.

Example

The first listing shows the module ExampleModule? which has a constant declaration v_exampleVariable at module scope. This variable is only used by the test case tc_exampleTestCase. Hence, it can be moved from the module scope into the component.

module ExampleModule {
	const charstring v_exampleConstant := "teststring"; 

	type component TestComponent { 
		// ...
	} 
	// ...
	testcase tc_exampleTestCase() runs on TestComponent { 
		// ...
		f_doSomething(v_exampleConstant); 
	}
}

The second listing shows the refactored version. Instead of a declaration at module level, the constant declaration is now part of the component.

module ExampleModule {
	type component TestComponent { 
		// ...
		const charstring v_exampleConstant := "teststring"; 
	} 
	// ...
	testcase tc_exampleTestCase() runs on TestComponent {
		// ...
		f_doSomething(v_exampleConstant);
	}
}

Back to TTCN-3 Refactoring Catalog