It’s common to use a centralised “Constants” class where you gather together lots of constants for your app. For example, in Objective-C you’ll often have a big file Constants.h full of lines like this.
#define DEBUG_MODE NO #define APP_URL @"http://itunes.com/apps/mycoolapp" #define CHARACTER_ANIMATION_TIME 2.0f #define FOOTER_IMAGE @"mycoolapp-footer.png" #define FONT @"Helvetica-Bold"
Don’t do this.
1. It increases compile times significantly. Because lots of other classes depend on Constants.h (it’s common to include it in the .pch header file so all files have access to it), any small change to Constants.h means that every class in your application will need to be recompiled. And ironically, during development at least, constants are often variable 🙂
2. It makes unit testing more complex, adds additional dependencies and discourages re-use. Because many classes depend on Constants.h, its harder to test them in isolation. Moreover, you can’t easily transfer classes between projects, without manually checking which parts of Constants.h needs to be copied too.
Instead, it’s much better to place constants in the class which they are used in. If you have a true need for a global constant, but it is for a value which you might want to frequently change during development (such as the DEBUG_MODE boolean above), consider using a macro to load the value from a config file, for example you could define a key in the Info.plist:
#define DEBUG_MODE ([[[[NSBundle mainBundle] infoDictionary] objectForKey:@"APP_MODE"] isEqualToString:@"DEBUG"])