Objective-C: Functional Macro Vs. method

In Objective-C, when do you recommend using function macros over class or instance methods?

+6
source share
4 answers

These are 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 use the fact that a short macro can expand into a piece of code, but rather a cheap substitute for C ++ style templates. Matthew Gallagher's matte tie is one example. And, like templates, overuse of large macros can lead to unexpectedly large code and a big debugging headache.

Besides constants and tiny expressions, if you can use a function or method instead of a macro, you probably should.

+5
source

In my opinion, never. Macros are text substitution tools that work at the compiler level, not when executing code. This is useful for determining constants, etc., but not for more complex things.

Functional macros seem to be used when there are a lot of repetitions in the code, so you do not need to support the same procedure in 13 different places. However, if you can successfully use the macro in this way, your code is poorly organized - you will most likely be better off reorganizing it and creating a single method for its implementation.

+3
source

It's hard to understand what you are actually asking, but I would recommend functions on macros as a general rule, as they perform type checking, show clear intentions and make debugging easier.

+2
source

I think Macros are better when you perform sensitive operations, as this makes debugging difficult.

For example, instead of using a method such as isJailBroken () that returns a boolean, a macro can be used to determine if the device is a Jailbroken.

This makes it difficult for an attacker to perform manipulations!

-1
source

Source: https://habr.com/ru/post/885663/


All Articles