I have many classes in the system that I'm currently developing and in these classes I have an array about the "name" of something. The name should be at most 30 characters.
Initially I used just 10 characters but now I need to increase the limit. Increasing the limit takes time though because I use this kind of array in many places. It would be easier if I used #define NAME_SIZE 30
or something like that and then all I would have to do is change one number instead of around twenty.
However I'm not sure if that's a "legal" thing to do in C++.
It would save me tons of time in the future, that's why I'm asking.
Yes, there is nothing technically wrong with it, except that #define
is usually inferior to a const std::size_t MAX_NAME_SIZE = 30;
Even better would be to have a dynamic size, e.g. using std::string
.
Scott Meyers has an interesting column about systems that use gratuitous fixed sizes, called The Keyhole Problem
The Keyhole Problem arises every time software artificially restricts something you want to see or something you want to express. If you want to see an image, but your image-viewing software artificially restricts how much of that image you can see at a time, that’s the keyhole problem. If you want to specify a password of a particular length, but your software says it’s too long, that’s the keyhole problem. If you want to type in your U.S. telephone number, but your software refuses to let you punctuate it in the conventional manner with a dash between the three-digit prefix and the four-digit exchange, that’s the keyhole problem.
Apart from annoynance from users, you also open your systems to all sorts of security issues (e.g. buffer overflow exploits).