#define or extern

Discussion in 'iOS Programming' started by mpramodjain, Dec 28, 2010.

  1. mpramodjain macrumors regular

    Joined:
    Nov 20, 2008
    Location:
    Banglore
    #1
    Hi,

    Just a programming basic quest, I do have with the experience of different projects , to be right different coding styles.

    To have some variable and its value with following specifications:

    i) Should be constant
    ii) Should be globally available.
    iii) Should not be causing any memory issues.

    I have seen most of the projects using NSString constant value with extern scope, and AFAIK this can also be done with using preprocessor directives (#define).

    After a bit of googling,.. I found that preprocessor directives are processed in the compile time.

    So does this make the build (or the last product .app, .exe in general programming) size more or no effect.

    Also please share, the pros and cons in the different scenarios.

    Thank you.
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    In almost all cases for a normal iPhone app there's no practical difference between using an extern const string and a #define string constant.

    The linker will combine multiple uses of a #define so that only a single string exists in the binary.

    Apple uses extern string constants but they are building shared libraries that are meant to be used by other code without rebuilding. Your app will always be rebuilt if you change the value of a string constant.
     
  3. mpramodjain thread starter macrumors regular

    Joined:
    Nov 20, 2008
    Location:
    Banglore
    #3
    Thanks for the quick reply.

    So, if we want create any generic library/Shared code as a third party (as facebook, Three 20 , any analytics, xml parser, json parser) it would be better to go with extern const variables , else go with #define is fine.

    Also, can you please make it clear a bit, without referencing to multiple building of the code or any link on the web for these kind of optimized programming tips would be great help.
     
  4. Mac_Max macrumors 6502

    Joined:
    Mar 8, 2004
    #4
    Another thing to remember is that #define simply does text replacement. Using extern will gain you type checking. An extern variable needs a piece of memory whereas a #define substitution doesn't (not really an issue unless you're developing for an embedded system and memory is at a premium). Choose based on your needs/requirements.
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    Pretty much the only advantage of an extern const string is that the value of the string could be changed in future versions of the library and existing apps will still work. Apps that link against a library will link against the string, not the value of the string. As a practical matter I'm not aware of any cases where apple changed the value of a string constant that's present in their public headers from one OS release to the next.

    If application code uses #define then the value is of course set at compile/link time, while if it uses extern const then the value can be set at run time. If a library were to use #define and the value were to change in a new release then the app and the library would be out of sync. If the app uses extern const then it doesn't matter if the value of the string changes in the library.

    In your own app it just isn't possible to release your library, with its string constants, separately from your app. The same is the case for any third party library like 320. The string constants will always be linked into your app not linked separately. It isn't possible for the app and the library to be out of sync.

    Basically, unless you're Apple, it doesn't matter which of these techniques you use. Just make sure that you use the macro or string constant name in your code and never use the value directly.
     
  6. mpramodjain thread starter macrumors regular

    Joined:
    Nov 20, 2008
    Location:
    Banglore

Share This Page