M
Michael Winter
In a recent post ("About C error" by Victor, 21 Sep 2003), comments were
made about the poster's use of macros. What I would like to know is why
they are considered bad? I'm not referring to their use as 'functions'; I
realise the loss of type-safety and the possible evaluation errors that can
occur. However, what would be the harm with numeric and text literals?
Consider a number that plays a significant role in the implementation of an
algorithm, and in that implementation the number is required in two, or
more, different scopes. There wouldn't be much point in declaring a
const-qualified variable in both scopes as it spoils a benefit of
identifying such 'magic' numbers - defining it in a single, easily locatable
position (for maintenance). In such a case, would you even consider waiving
the 'no globals' recommendation just to avoid the use of a macro?
Would performance, or in fact anything, really be adversely affected by the
use of a macro instead of a const-qualified variable?
Just out of plain curiosity (as I use macros, as described above, myself),
Mike
--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
The comments I refer to follow. Please note that no offence to the posters
is intended. I am simply using their comments as examples (I don't have any
others to hand).
Kevin Goodsell (in response to the original post):
Macros are bad.
-----
Jack Klein (in response to the original post):
Why are you putting parentheses around simple text macros like this?
It can't do you any good, and it just might cause problems in some
instances, although I can't think of any off-hand.
Since you are compiling with a C++ compiler, why not use constant
variables instead?
made about the poster's use of macros. What I would like to know is why
they are considered bad? I'm not referring to their use as 'functions'; I
realise the loss of type-safety and the possible evaluation errors that can
occur. However, what would be the harm with numeric and text literals?
Consider a number that plays a significant role in the implementation of an
algorithm, and in that implementation the number is required in two, or
more, different scopes. There wouldn't be much point in declaring a
const-qualified variable in both scopes as it spoils a benefit of
identifying such 'magic' numbers - defining it in a single, easily locatable
position (for maintenance). In such a case, would you even consider waiving
the 'no globals' recommendation just to avoid the use of a macro?
Would performance, or in fact anything, really be adversely affected by the
use of a macro instead of a const-qualified variable?
Just out of plain curiosity (as I use macros, as described above, myself),
Mike
--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
The comments I refer to follow. Please note that no offence to the posters
is intended. I am simply using their comments as examples (I don't have any
others to hand).
Kevin Goodsell (in response to the original post):
#define MAXVERTICES (20) /*Maximum number of object's vertices*/
#define MAXFILENAME (20) /*Maximum characters of file's name*/
#define MAXDATA (40) //Maximum data can be read
#define M (256)
#define N (256)
Macros are bad.
-----
Jack Klein (in response to the original post):
#define MAXVERTICES (20) /*Maximum number of object's vertices*/
#define MAXFILENAME (20) /*Maximum characters of file's name*/
#define MAXDATA (40) //Maximum data can be read
#define M (256)
#define N (256)
Why are you putting parentheses around simple text macros like this?
It can't do you any good, and it just might cause problems in some
instances, although I can't think of any off-hand.
Since you are compiling with a C++ compiler, why not use constant
variables instead?