I'm aware of the pitfalls of macros and avoid them when what I really want is a function.
What I've found them helpful for, though, is storing things like "magic" numbers, file names, font names and so on. So things like:
ProjectHeader.h
#ifndef __PROJECT_HEADER__
#define __PROJECT_HEADER__
#define kXMLFileName "scoresAndSettings.xml"
#define AUDIO_DATA_TYPE_FORMAT SInt16
#define NUM_AUDIO_BUFFERS 3
#define AUDIO_SAMPLE_RATE 44100.
#define NUM_AUDIO_CHANNELS 2
#define kGameFont @"Helvetica-Bold"
#define kGameFontSizeNormal 18
#define kGameFontSizeSmall 16
#define kGameFontSizeTiny 11
These let me (a) store UI specifics in one place where I can change them and know the change will propagate throughout the code, (b) give them a name that describes their function in the code, and (c) use code autocompletion to know that I've actually typed the right term.
I feel fairly confident that this isn't a terrible way to work, but I'd like to know if someone thinks it is, and if so how to do it better.
These are perhaps a little dodgier but I still find them very helpful:
#define __COPY_PROTECTION__
#define __SHOW_FPS__ NO
#define __SKIP_LAUNCH_SCREEN__ 0
#define __START_IN_GAMEPLAY__ 1
#define __START_IN_PREFS__ 0
#define __START_IN_WIN_SCENE__ 0
#define __AUTO_WIN_TESTING__ 0
(In various points, then, in the code:
AppDelegate.h
if (__START_IN_GAMEPLAY__) {
[self show:MyGameplay];
return;
}
[self show:MainScreen];
). This allows me to jump straight into testing whatever part of the project I'm working on, by manipulating only macros #define
d in the ProjectHeader.h
file.
Is this good? Bad? Is there a better way for next time?