Search code examples
iosobjective-cconstantsc-preprocessorifndef

ifndef, define & direct assignment of constants


I am just thinking of the difference between below methods, while defining constants:

Method1: Create a header file to define all the constants, using include guard:

#ifndef c1
#define c1 @"a123456789"
#endif

then assign the constant to the function:

Identity.number = c1;

Method2: Just simply define the constant

#define c1 @"a123456789"

then assign the constant to the function:

Identity.number = c1;

Method3: Do not define a constant, just assign the value to a function:

Identity.number = @"a123456789";

Any pros and cons for the above?


Solution

  • Methods 1 and 2 are much better for bigger projects, because you can easily change the value of the constant one place. Method 1 may be especially good for very big projects with many files, but is not really necessary for smaller projects.

    In method 3, you have to search through every line of code to find the value you want to assign to (if you assign it more places). Therefore, I think it is bad to use this.