Re: Using multiple defines!

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Robby wrote:
int FETCH(
int *A2,
int *A1,
int *A0,
int MSG) {

//aGMI declared in header (Not shown!)
#define __CW aGMI[MSG-1].CHARWIDTH
#define __LP aGMI[MSG-1].LEADING_PIX
#define __TP aGMI[MSG-1].TRAILING_PIX
#define __CS aGMI[MSG-1].CHARSPACING

#define __TCW ((__CW -__LP)-__TP)+ __CS

Three things here:
1. Macros are evil. You have to be aware that they don't respect anything
like scope but are simple text replacements. Putting them into a sourcefile
to auto-generate some code can be justifyable, but in a header like that
rarely is.
2. Names with two consecutive underscores are reserved, dito names beginning
with an underscore followed by a capital letter.
3. Leave ALL_UPPERCASE for macros and use only that for all macros. The
mixture between __CW and MSG is surely a bad idea. The reason is that it
makes spotting whether something is a macro much easier.

Since those are all shortcuts to access a field in a structure that itself
is element of an array, you could possibly easier write it like this:

struct Character* pc = aGMI[msg-1];
int w = pc->charwidth;

Also, instead of __TCW, you could use:

inline int total_char_width( struct Character const* pc)
{
return pc->charwidth
- pc->leading_pix
- pc->trailing_pix
+ pc->charspacing;
}


... we end up with these long ugly accessings like:

aGMI[MSG-1].CHARWIDTH

To be honest, I wouldn't call this ugly. The point is that /you/ can /now/
read a shorter syntax, but for someone that isn't intimately familiar with
the code the above is much clearer than '__CW'!

in for loops, function parameters and all kinds of expressions which makes
the code very difficult to follow a few months from now. Thats why I
wanted to declare everything at the top with define statements. How do you
guys get all these formulas organized?

Hmmm, here you actually claim the opposite of what I claimed above. Well,
well have to agree to disagree there. ;)

Uli

.



Relevant Pages

  • RE: Using multiple defines!
    ... //aGMI declared in header ... Best regards ... int FETCH( ... to declare everything at the top with define statements. ...
    (microsoft.public.vc.language)
  • Re: S-expr form of C
    ... rpw3@xxxxxxxx (Rob Warnock) writes: ... and have macros defined to build these CLOS ... to ground such an effort firmly in some concrete syntax which *can* ... (let ((av int) ...
    (comp.lang.lisp)
  • Re: error: implicit declaration of function `func_name
    ... If your function is "int foo" either declare that in a header ... a standard complying compiler ...
    (comp.lang.c)
  • Re: Prototyping rules/recommendations sought
    ... >> internal functions but just declare them inline so to speak. ... > int main ... In this case you should also declare foo() as being static, ... the header contains only things meant to be visible to other ...
    (comp.lang.c)
  • Re: Portable printf specifier
    ... > For printf, int uses %d, long uses %ld. ... The C99 approach is to add a second header,, that defines ... macros for *printfand *scanfconversion specifiers for these ...
    (comp.lang.c.moderated)