Stripping code of #define

59 Views Asked by At

I'm looking at a ton of code like this:

#define CONSUMPTION_MODE_MOBILE 0
#define CONSUMPTION_MODE_WIFI 1
#define CONSUMPTION_MODE_COMBINED 2

What would be the best to change this to? Enum? Individual const int's? Or something else?

I have little experience of preprocessor stuff (and not that much with Xcode, so consider this a close-to-noobie question), but I am reasonably sure I don't need this hanging round the codebase I've inherited. Not sure what is best practice, however.

1

There are 1 best solutions below

0
On

"Best" is a very subjective concept, the best that you could hope for :-) would be to understand the pros and cons of each possibility.

There's absolutely nothing wrong with using #define like that to create constants, the only thing I don't like about it is that you often don't get the "symbols" passed all the way through to a debugger. But that's actually nothing to do with the language per se, more to do with the build tools you're using.

And there's certainly nothing wrong with leaving code alone if it works fine. I wouldn't be changing something just because you think there's a cleaner way, since that always runs the risk of introducing new problems.

If you find yourself ever having to make substantial changes to the relevant code for some other reason, that's when I'd consider cleaning up as well, tempered against the possible impact it may have elsewhere.