When do you use "if ( a = b )"? (was Re: Funny mistake)
Stephen Clamage
steve at taumet.com
Fri Mar 29 01:57:45 AEST 1991
dmm at cfa.harvard.edu (David Meleedy) writes:
>I would like to respond to the view that statements such as
>#define EQU ==
>are to be avoided.
>I disagree entirely. One of the well known conventions in C is that anything
>in all capital letters is defined in a define statement. It is the
>responsibility of the programmer to know to look up and see what various
>defines are.
This is a limited view, IMHO. Here is an example from an actual program:
int i AND j;
You could sort of see what the programmer had in mind -- except that && would
be illegal in this context, so maybe 'int i, j;' was intended. It turned
out that the program had the line
#define AND ; long
so the code really meant
int i; long j;
How many include files had to be scanned to find this abomination?
Once you start using macros for standard language constructs, where do you
stop?
#define BEGIN {
#define END {
#define AND &&
#define OR ||
#define IF if
#define THEN {
#define ELSE } else {
#define ELSEIF } else if
#define ENDIF }
Some would claim that this set of defines makes for a much more readable
program. It is not easier for experienced C programmers who have to look
up and mentally translate these constructs into standard C. The original
programmer may have found a set of unusual macros convenient after becoming
accustomed to programming with them, but what about those who come later
and have to maintain the code?
Finally, there are a lot of tools which do simple syntax scans of programs
to provide various analyses of the code. What makes these convenient
is that they can scan files without having to do a full preprocessor
pass first. If you subvert the standard syntax of C, these tools become
less convenient or impossible to use.
--
Steve Clamage, TauMetric Corp, steve at taumet.com
More information about the Comp.lang.c
mailing list