|
6.4 The Case of the Small Function Often, when programming, there is a need for a code abstraction facility that operates at the text level. If the statement "a:=3;" occurs often, it may be best simply to repeat it verbatim. If a sequence of one hundred statements is repeated often, it is normal to remove the code to a function and replace the occurrences by a function call. However, in between these two extremes are cases where a particular sequence of code is long enough and appears often enough to be troublesome, but which is bound so messily to its environment as to make a function call cumbersome. For example, the following line of statements (referring to five variables declared local to a function) might appear ten times in a function:
a=b*3.14159; c=d % 256; e=e+1; Now the "normal" rule of programming says that these statements should be placed in a procedure (also called a "function" in the C programming language used in this example), but here five local variables are used. Use of a procedure (function) would result in a procedure definition looking something like:
void frobit(a,b,c,d,e) float *a,b; int *c,d; unsigned *e; {*a=b << 8; *c=d % 256; *e=*e+1;} and a procedure call something like
frobit(&a,b,&c,d,&e); This might be workable in a language that allowed formal parameters to be specified to be bound only to particular variables. Similarly, it might be possible to avoid the parameter list in languages that support local procedures that can access non-local variables (such as Pascal). However, in our example here, in the C programming language, these options are not available, and so we must either create a function with five parameters, or use the C macro preprocessor (the best solution). FunnelWeb provides the same macro facility for languages that do not have a built-in preprocessor. In particularly speed-stressed applications, the programmer may be reluctant to remove code to a procedure because of the procedure-call overhead. FunnelWeb macros can help there too. In summary, there sometimes arises in programming situations where the cost of defining a procedure is higher than the benefits it will bestow. Common reasons for this are the run-time procedure overhead and the messy binding problems caused by removing target code from its target context. FunnelWeb can help in these situations by allowing the programmer to define a text macro. This avoids all the problems and provides an additional incentive for the programmer to describe the piece of code so isolated.
Webmaster Copyright © Ross N. Williams 1992,1999. All rights reserved. |