#define vs enum



I notice that in the glib/gdk/gtk headers there are some #define's of
constants.  I'd like to suggest that this is perhaps BadPractice(tm),
or at least NonOptimal(tm).

We've already committed to an ansi C compiler by using naked
prototypes in the headers.  The only reasons to use #define's for
constants is either to patronize old or broken compilers, or to save a
coupla bytes in embedded apps.

If we're committed to ansi, then the old/broken compiler consideration
is a non-issue.  In which case, enum's are a better bet than #define's
for two main reasons.

1) they can be type checked, avoiding many cast requirements, and
   offering compiler support correct usage.

2) the value of an enum token is embedded in the object file and can
   be printed with a debugger.

For the honest-to-goodness single stray constant, "const" is a
possible alternative.  It allows the debugger to print the value,
though it costs a few bytes in the data section to do so.

If there's clear agreement on enums, then I'll volunteer to make the
changes throughout glib/gdk/gtk/gimp, and maybe even gnome.

--rich



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]