3

I have some strings in my project and I want to put all the strings in a common file say constants.

(1st Approach) In constants.h, I can do :

    #define COMMON_STRING @"myString"

OR

(2nd Approach)

In constants.h

    extern NSString *const COMMON_STRING;

In constants.m

    NSString *const COMMON_STRING = @"myString";

Which approach is better to use and why?or do we have some other better approach for this?

EDIT:

According to that post extern NSString *const COMMON_STRING; is better in terms of memory perspective. But somewhere I have also read that The #define will insert the string into all the occurrences, by that - multiplying the memory usage unless compiler optimizing same constant string occurrences — which is the case.So does it make sense not to use #define?

In the suggested posts one post only defines the structure and other is explaining the comparison but very limited and not what I am expecting.If compiler is optimizing same constant string occurrences then why to use extern NSString *const COMMON_STRING and not #define COMMON_STRING?

Apoorv
  • 362
  • 5
  • 12
  • 1
    According to that post extern NSString *const COMMON_STRING; is better in terms of memory perspective. But somewhere I have also read that The #define will insert the string into all the occurrences, by that - multiplying the memory usage unless compiler optimizing same constant string occurrences — which is the case.So does it make sense not to use #define? – Apoorv Aug 25 '13 at 15:18
  • 1
    Memory-wise they are the same. The compiler will only make one string that says @"foo" no matter how many times you hard code it. All uses will use that immutable string. There may be non memory reasons to use one or the other. – chadbag May 22 '14 at 00:16

1 Answers1

2

Generally speaking:

static const

It respects scope and it is type-safe.

The only limitation I could see was that if you want the variable to be possibly defined on the command line.

But there is still an alternative:

#ifdef VAR // Very bad name, not long enough, too general, etc..
static int const var = VAR;
#else
static int const var = 5; // default value
#endif

Whenever possible, instead of macros / ellipsis, use a type-safe alternative.

If you really NEED to go with a macro (for example, you want FILE or LINE), then you'd better name your macro VERY carefully because in its naming convention Boost recommends all upper-case, beginning by the name of the project (here BOOST_), while using the library you will notice this is (generally) followed by the name of the particular area (library) then with a meaningful name.

anky_believeMe
  • 484
  • 1
  • 4
  • 14
  • What does "type safe" have to do with anything? The order of his qualifiers indicates that the type of the value is immutable, yes, but it's not a constant. const has to apply to the pointer for that. – CodaFi Aug 25 '13 at 15:19
  • also with #define there's a general danger of mangling code as the preprocessor is not aware of syntax. – anky_believeMe Aug 25 '13 at 15:27
  • @believe_anky do you mean to say preprocessor is not aware of the type means it could be int or string.is it? – Apoorv Aug 25 '13 at 15:54
  • 1
    When using `#define` the identifier gets replaced by the specified value by the compiler, before the code is turned into binary. This means that the compiler makes the substitution when you compile the application. When you use const and the application runs, memory is allocated for the constant and the value gets replaced when the applicaton is ran. – anky_believeMe Aug 25 '13 at 15:58