Advantages of Macros
A list of the advantages of macros reads like the list of
advantages for using procedures.
The two mechanisms---procedure calls and macro expansion---are
completely different.
But the resulting effect is often similar.
Advantages of Macros
- Macros hold the details of an operation in a module that
can be used "as if" it were a single instruction.
- A frequently used sequence of instructions can be defined
as a macro.
- Now the macro can be used rather than retyping the sequence.
- The sequence can be debugged just once, rather than each
time it is typed in.
- By using acual arguments, variations of the same idea
are implemented.
- Macros are used to build up complex operations out of
simpler operations.
- Libraries of useful macros can be created. Programmers can
use these libraries without knowing much about the detailed
code the macros expand into.
The above list is not complete, and most of the reasons overlap.
The whole list can be summarized by saying that the advantage of
macros is "modularity."
Of course, "modularity" also summarizes the advantages of
procedure calls, of objects, and of structured programming.
QUESTION 9:
A complicated operation has been implemented using about 1000 lines
of code. The operation is expected to be used many times in a
typical application program. The programmer proposes to turn the code
into a macro and put it in a macro library.
Is this a good idea?
ANSWERS:
- No---expanded source files using the macro will get too large.
- No---since the macro is large it should not be placed in a library.
- Yes--users will not want to see the details,
so the modularity of a macro is a good idea.
- Yes--once it is in a library all programmers can use it.