wiki:RefactoringSteps/MoveMacro

Version 1 (modified by manualwiki, 13 years ago) (diff)

--

Move macro

This transformation moves macro definitions between two files. Source and target files can be either modules or header files, the conditions are slightly different in every case. The goal of the transformation is to make the moved macro definitions available in every place where they are used.

Move macro example:

%%header.hrl

-define(Ok, ok).
%%mm.erl

-module(mm).
-define(Person(Name,Age),
        {Name,Age}).

f() -> ?Person("John",33).

Result:

%%header.hrl

-define(Ok, ok).
-define(Person(Name,Age),
        {Name,Age}).
%%mm.erl

-module(mm).
-include("client.hrl").

f() -> ?Person("John",33).

Side conditions

  • The names of the macros to be moved must not clash with existing macro names in none of:
    • the target file,
    • the target’s included files,
    • files where the target is included.
  • If the user does not specify the macros to be moved, the transformation starts an interaction to ask the user to specify macros. The user has to select the macros to be moved from a checkbox list.
  • Moving macros from a header to a module is only allowed if there exist no other module that both includes the header and uses some of the macros to be moved.
  • An include form can only by introduced when it does not cause inconsistency at the place of inclusion.

Transformation steps and compensations

  1. The macro definitions are removed from the source file.

  1. If the target is a header file that does not exist, the file is created.
  1. The macro definitions are placed at the end of the target header file, or before the first function of the target module file.
  1. If a macro is moved into a header file, then every module that uses the record is changed to include the target header file. This is not an issue when the target is a module file.