In Objective-C, when do you recommend using function-like macros over class or instance methods?
4 Answers
They're very different things. A function or method exists exactly once in your code; a macro inserts its entire definition into your code every time you use it.
Some people take advantage of the fact that a short macro can expand into a chunk of code, rather like a cheap substitute for C++ style templates. Matt Gallagher's singleton macro is one example. And like templates, overuse of large macros can lead to surprisingly large code and big debugging headaches.
Aside from constants and tiny expressions, if you can use a function or method in place of a macro, you probably should.
Comments
In my opinion, never. Macros are effectively text substitution tools that work at the compiler level, not while executing your code. This is useful for defining constants and such, but not good for more complex things.
Function-like macros seem to be used when there is a lot of repetition in the code, so you don't have to maintain the same routine in thirteen different places. However, if you can successfully use a macro in such a way, your code is poorly organized - you'd likely be better off refactoring it, and creating a single method to implement it.
Comments
I think Macros are better when you do sensitive operations, since it makes difficult for debugging.
For example, instead of using a method like isJailBroken() that returns a Boolean value, a Macro can be used to find out if the device is Jailbroken.
This makes the attacker's job difficult to do run-time manipulations!