Re: Using multiple defines!
- From: Ulrich Eckhardt <eckhardt@xxxxxxxxxxxxxx>
- Date: Fri, 14 Dec 2007 09:25:01 +0100
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
.
- References:
- Using multiple defines!
- From: Robby
- Using multiple defines!
- Prev by Date: Re: Message between WM_CREATE and WM_PAINT
- Next by Date: Re: How to find files using only part of the files name?
- Previous by thread: Re: Using multiple defines!
- Next by thread: Re: A non-const reference may only be bound to an lvalue?
- Index(es):
Relevant Pages
|