0

Possible Duplicate:
C++ - enum vs. const vs. #define

In the Linux kernel, one can see this:

enum {
  CONST_X = 1,
  CONST_Y = 2,
};

Is that a better practice than #define CONST_X 1?

Community
  • 1
  • 1
Cartesius00
  • 23,584
  • 43
  • 124
  • 195
  • The linux kernel is by no means a c style guide. A lot of the stuff in there is micro optimization for performance you would never do as an application programmer. – Byron Whitlock Oct 07 '11 at 19:58
  • 1
    Yes. Preprocessor macros are to be avoided when there are perfectly valid ways of doing the same thing in the language itself. – user229044 Oct 07 '11 at 19:59
  • WHat are you planning to do with these values? – bmargulies Oct 07 '11 at 20:01
  • 1
    Related: http://stackoverflow.com/questions/7571019/when-to-use-enums/, http://stackoverflow.com/questions/136946/difference-between-enum-and-define-statements, http://stackoverflow.com/questions/4767667/c-enum-vs-const-vs-define – Steve Jessop Oct 07 '11 at 20:03
  • 1
    @Chris This is not fanaticism, but it certainly looks more like fanatical defense of `#define` on your part. When the language provides a built-in facility, don't reinvent it with preprocessor macros. Simple. I just assumed the whole world was on board with what is inarguably a best practice. There is literally no reason to `#define` your constants. It doesn't save key strokes, or produce cleaner code, or reduce the complexity, or increase performance from a cpu-cycles or memory usage stand point. – user229044 Oct 07 '11 at 20:13
  • See also http://stackoverflow.com/questions/1674032/static-const-vs-define-in-c – Fred Larson Oct 07 '11 at 20:23
  • 1
    @meagar If you #define them then they are literals and so true constants. The same can be said for the `enum` approach, but not the use of `const`. It is disappointing that the question was closed as a duplicate of a question discussing C++, an entirely different language, especially in this area. The big problem I have with enum is the complete lack of type safety. I think it reads better at the declaration but thereafter the language abandons you to your devices. – David Heffernan Oct 07 '11 at 22:18
  • @meagar: there literally is a reason - to use them in preprocessor `#if` conditions. Accusations of fanaticism aside, it's simply incorrect to assume that's never useful, and it's one good reason why so many constants in the standard (`INT_MAX` and so on) are macros, not enum members. – Steve Jessop Oct 07 '11 at 22:32
  • @Steve You missed the earlier conversation, but we've already covered this ground: *Obviously* if you intend for your constant to interact with the preprocessor, you don't really have any other choice than `#define`, do you? Nobody is arguing otherwise, and I thought it was obvious enough to not state it. – user229044 Oct 11 '11 at 16:34
  • @meagar: Well, I'd say "use `#define` unless you're somehow absolutely certain nobody will ever find a use of the value in the preprocessor". Your talk of inarguable best practices and "literally no reason" gave me the impression that you wouldn't agree with that. – Steve Jessop Oct 12 '11 at 08:49

0 Answers0