Advertisement

Are #define good to use?

Started by May 14, 2002 08:12 PM
37 comments, last by The Lion King 22 years, 7 months ago
Hi, Is it a good habbit to use pre-defined values in replacement of Variables, if the variable has to remain constant in all of the program. e.g. if I am giving name to the WNDCLASSEX Class i.e. WinClass.lpszClassName = "New Window"; I have three options instead of directly using string here. * Declare, a constant char array and store the string in it. * Use #define NAME "My Window" * or copy the string from #define NAME to the char array. Which one is good for good programming habbits? I can survive anything ... even NUKES!!! The Lion King
The trouble with defined values is that they lack type information. The practice is acceptable in C. In C++ the preference is "const int varname;". In the case of strings, the preprocessor will replace the define with the string - thus a string literal. In C it won''t likely matter much. In C++ - the sticklers will likely complain. If you go with the defines - wrap the string in the TEXT() macro. Consult the C++ faq for greater detail.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Advertisement
#defines in place of constants can be a bit dangerous, the only time you should use #define is for setting debug states and making include guards in your header files.
The bigger problems with #defines are that they do not respect scope boundaries, thus violate the rule of thumb "define variables in the narrowest applicable scope". Also, since they are preprocessed, they compromise the ability of tools to provide useful information, such as compiler diagnostics.

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]
If #DEFINE is so bad (and I''m not disputing that), why do Microsoft''s DirectX headers for C++ use them so much?
[ PGD - The Home of Pascal Game Development! ] [ Help GameDev.net fight cancer ]
quote: Original post by Useless Hacker
If #DEFINE is so bad (and I'm not disputing that), why do Microsoft's DirectX headers for C++ use them so much?


I'll say only this: M$ is not a reference when programming. DX headers suck big time( I mean the code that's in them looks like crap ) and it's full of macros to simplify programming. That and the fact that most programmers at M$ are tought how to code by M$. If we could look at the Windows source code, I'm sure we'd see some #DEFINE also. One other thing, don't take M$ for programming gods, they aren't and they'll sure as hell never be.

"And that's the bottom line cause I said so!"

Cyberdrek
danielc@iquebec.com
Founder
Laval Linux

/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash

[edited by - cyberdrek on May 15, 2002 7:17:59 AM]
[Cyberdrek | ]
Advertisement
#define can cause hard to find bugs. So, even if you are still programming in straight C, you should still use const instead, because it adds type safty and reduces hard to find bugs.

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
MS are notorious for the poor programming style exhibited in their own libraries.

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]
Just a side comment on defines: despite the fact that they have a set of disadvantages and drawbacks, they are by far not useless.

In the context The Lion King asked his question, yes I agree w/ the other posters.
Forever trusting who we areAnd nothing else matters - Metallica
I could very possibly be wrong, but it occurs to me that the reason why Microsoft''s headers use #defines could be that they are used for C (which, as I recall, does not support const-type constants) as well. I don''t know this, but I do know that DirectX can be invoked in C as well as C++.

This topic is closed to new replies.

Advertisement