Steve Harrison Steve Harrison - 26 days ago 6
Objective-C Question

What are the benefits of placing a #define statement in a header (.h) file?

Is there any benefit to placing the

preprocessor statement in the header file
as opposed to the implementation file

I have legacy code with
statements sprinkled in both. Are these statements affected by scope?

Rob Rob
Answer Source

There are no benefits of having the #define in .h file other than, obviously, making them available in multiple .m files.

So, if it's something that potentially (even if you're not doing so yet) could be useful to multiple .m files, then put in in .h file. If it's something that could only be of value to a single .m file, then put that in the .m file.

A corollary of the above: Avoid repeating a #define in multiple .m files: If you have the same #define in multiple .m files, then that's a candidate of something that should be in a .h file.

In a comment you contemplate whether there is any "negative impact to grouping [them] together". If you're contemplating moving them to the .h file, I'd discourage that. Keep the scope as narrow as possible/logical. If you're contemplating moving them to the .m file, the question is whether any of these constants are potentially useful to a broader context.

Bottom line, I would not advise consolidating these to a single area just for the sake of keeping them together. Let the functional intent of the constant dictate where it goes.