Ditch the #define “constants”

If you find yourself using a lot of #define X 10 (or other integers) for constants in your Objective-C code, stop! #define is parsed by the preprocessor and not the compiler(last I heard). It is more or less a search and replace of X with 10 by the preprocessor in the above example. It is not seen by the debugger or anything(since it is not a symbol). So, I would suggest enum.

enum values {first = 10} constant;

constant X = first;

This might be inconvenient if you only have a couple of #defines laying around, but it is better to use enums instead of large chunks of #define (even Apple uses enums ! — check the QuickTime 7.1 reference). Also, the author of Magic Number Machine (Matt Gallagher) uses enums extensively

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s