Is #define banned in industry standards?

前端 未结 13 2796
长发绾君心
长发绾君心 2020-12-23 18:52

I am a first year computer science student and my professor said #define is banned in the industry standards along with #if, #ifdef, <

相关标签:
13条回答
  • 2020-12-23 19:15

    That's completely false, macros are heavily used in C. Beginners often use them badly but that's not a reason to ban them from industry. A classic bad usage is #define succesor(n) n + 1. If you expect 2 * successor(9) to give 20, then you're wrong because that expression will be translated as 2 * 9 + 1 i.e. 19 not 20. Use parenthesis to get the expected result.

    0 讨论(0)
  • 2020-12-23 19:18

    If you had just mentioned #define, I would have thought maybe he was alluding to its use for enumerations, which are better off using enum to avoid stupid errors such as assigning the same numerical value twice.

    Note that even for this situation, it is sometimes better to use #defines than enums, for instance if you rely on numerical values exchanged with other systems and the actual values must stay the same even if you add/delete constants (for compatibility).

    However, adding that #if, #ifdef, etc. should not be used either is just weird. Of course, they should probably not be abused, but in real life there are dozens of reasons to use them.

    What he may have meant could be that (where appropriate), you should not hardcode behaviour in the source (which would require re-compilation to get a different behaviour), but rather use some form of run-time configuration instead.

    That's the only interpretation I could think of that would make sense.

    0 讨论(0)
  • 2020-12-23 19:21

    No, #define is not banned. Misuse of #define, however, may be frowned upon.

    For instance, you may use

    #define DEBUG

    in your code so that later on, you can designate parts of your code for conditional compilation using #ifdef DEBUG, for debug purposes only. I don't think anyone in his right mind would want to ban something like this. Macros defined using #define are also used extensively in portable programs, to enable/disable compilation of platform-specific code.

    However, if you are using something like

    #define PI 3.141592653589793

    your teacher may rightfully point out that it is much better to declare PI as a constant with the appropriate type, e.g.,

    const double PI = 3.141592653589793;

    as it allows the compiler to do type checking when PI is used.

    Similarly (as mentioned by John Bode above), the use of function-like macros may be disapproved of, especially in C++ where templates can be used. So instead of

    #define SQ(X) ((X)*(X))

    consider using

    double SQ(double X) { return X * X; }

    or, in C++, better yet,

    template <typename T>T SQ(T X) { return X * X; }

    Once again, the idea is that by using the facilities of the language instead of the preprocessor, you allow the compiler to type check and also (possibly) generate better code.

    Once you have enough coding experience, you'll know exactly when it is appropriate to use #define. Until then, I think it is a good idea for your teacher to impose certain rules and coding standards, but preferably they themselves should know, and be able to explain, the reasons. A blanket ban on #define is nonsensical.

    0 讨论(0)
  • 2020-12-23 19:25

    No. It is not banned. And truth to be told, it is impossible to do non-trivial multi-platform code without it.

    0 讨论(0)
  • 2020-12-23 19:26

    If you want your C code to interoperate with C++ code, you will want to declare your externally visible symbols, such as function declarations, in the extern "C" namespace. This is often done using conditional compilation:

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    /* C header file body */
    
    #ifdef __cplusplus
    }
    #endif
    
    0 讨论(0)
  • 2020-12-23 19:28

    Some coding standards may discourage or even forbid the use of #define to create function-like macros that take arguments, like

    #define SQR(x) ((x)*(x))
    

    because a) such macros are not type-safe, and b) somebody will inevitably write SQR(x++), which is bad juju.

    Some standards may discourage or ban the use of #ifdefs for conditional compilation. For example, the following code uses conditional compilation to properly print out a size_t value. For C99 and later, you use the %zu conversion specifier; for C89 and earlier, you use %lu and cast the value to unsigned long:

    #if __STDC_VERSION__ >= 199901L
    #  define SIZE_T_CAST
    #  define SIZE_T_FMT "%zu"
    #else
    #  define SIZE_T_CAST (unsigned long)
    #  define SIZE_T_FMT "%lu"
    #endif
    ...
    printf( "sizeof foo = " SIZE_T_FMT "\n", SIZE_T_CAST sizeof foo );
    

    Some standards may mandate that instead of doing this, you implement the module twice, once for C89 and earlier, once for C99 and later:

    /* C89 version */
    printf( "sizeof foo = %lu\n", (unsigned long) sizeof foo );
    
    /* C99 version */
    printf( "sizeof foo = %zu\n", sizeof foo );
    

    and then let Make (or Ant, or whatever build tool you're using) deal with compiling and linking the correct version. For this example that would be ridiculous overkill, but I've seen code that was an untraceable rat's nest of #ifdefs that should have had that conditional code factored out into separate files.

    However, I am not aware of any company or industry group that has banned the use of preprocessor statements outright.

    0 讨论(0)
提交回复
热议问题