Importance of using const/#define in code
Ok this is something that could draw flak. I believe in using these a lot. In many cases I use
it excessively. But hey, no one was ever hurt for being too careful about code.
Ok imagine this piece of code:
1.cpp
DunkClass AllProcessIds[32];
2.cpp
extern DunkClass AllProcessIds[32];
Any idea as to how twisted this could get? Ok look @ this. Some coder A says that he loves the
number 31 because it is the last index in the array and his code requires that his manager be placed
@ the end of the list or he just hard coded it for fun. Whatever it is. Tomorrow I decide that we are
wasting too much space by doing a 32 sized array and I change the code:
1.cpp:
DunkClass AllProcessIds[16];
but
2.cpp:
extern DunkClass AllProcessIds[32];
Do you see the problem? My friend coder A will still be using the index 31 and thus memory that
is not allocated is being used here. In a decently complex system, this would wreck havoc.
In fact my policy is to have a cpp file that contains all the possible constant/enumerations etc;
This would generally be, error codes, enumerations, constants.
I felt that anything that could end up hardcoded has to be #defined/const ed. This has the added
benefit that the 32 would end up being:
#define MAX_PROCESS_IDS 32
or
const int MAX_PROCESS_IDS = 32;
when someone looks @ a variable like this, it becomes quite obvious as to what 32 means. Of course this
assumes that coder A uses the specification (MAX_PROCESS_IDS) instead of hard coding values himself.
Copyright CyraX/Chaitanya
http://students.iiit.net/~cyrax
