Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

The first method is important when you make sure that the constant is only defined once. The third method don't allow the IDE to help you with autocompletion which can be important when the value of the constant is more complex.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top