C ++ global constants

We have this set of "utility" constants defined in a series of files. The problem arises because TOO MANY files contain these global constant files, which, if we add a constant to one of these files and try to build, it creates the entire library, which takes more than an hour.

Can anyone suggest a better way for this approach? That would be very helpful.

+3
c ++ constants
source share
4 answers

First, if you define them directly in the header, I would suggest using extern const instead and then defining them in the cpp file:

//in .hpp: extern const std::string foo; //in .cpp: const std::string foo = "FOO"; 

Thus, at least the definitions can be changed without restoration.

Secondly, study where they are included. If the permalink is included in the low-level header, is it possible to include include in cpp instead? Removing it can lead to a decrease in communication, so he does not need to restore so much.

Thirdly, open this file. I would suggest displaying the structure that you ultimately want, start adding new constants to the new structure instead of the old one. In the end (when you are sure that you have the structure you want), reformat the old file to the new structure and make the old file included with the whole structure. Finally, go through and delete everything included in the old file, pointing them to the corresponding new sections. This will break the refactoring, so you don’t have to do it all at once.

And fourthly, you can trick your compiler to not rebuild if the header file changes. You will need to check the documentation of your compiler, and this may be unsafe, so you sometimes want to add complete assemblies as well.

+7
source share

Do you really need every global parameter included in every file? You should probably break constants into categories and then split them into different files.

Each .h that is included is simply copied at that moment in the file that includes it. If you change something in the file (either directly or by changing something that comes with the kit), you need to recompile it.

Another solution would be to have an .h file that has an accessory to the string values ​​/ values ​​map. Then, in the .cpp file of this card / accessor, you can insert new values. Each new value that you set will need only 1 file to recompile.

Another solution is to not include the header file anywhere. Just extern in the variables you need in every .cpp file.

+2
source share

Perhaps it's time to do some refactoring to improve cohesion and reduce coupling in your software. Separating global constant files will allow the module to be more selective about which constants should be included, which eliminates some of the unnecessary cohesion. As a last resort, you can completely split it into one constant per file and make sure that each module contains only the constants that it needs to use.

But this can lead to poor cohesion, in the sense that constants can naturally fall into related groups, so a module that requires one constant will usually also need many others from this group. Thus, the trick is to find the best grouping of constants in various global files, and then ensure that each module includes only what it needs.

+1
source share

(Edit: I did not think about external constants. In fact, my idea is a little silly.)

(Edit: my β€œsilly” macro idea actually saves build time when constants are added. Thanks for pointing this out, Brian!)

Using parallel build :-)

Seriously, I think one solution would be to create another header called utility_ex.hpp, or something where you add new constants that you sometimes merge into the .hpp utility (or something that gets called in the current constant header).

Another (less efficient) solution would be to have a macro like this:

 #define constant(name) get_constant(#name) // # means turn name into a string literal int get_constant(const char *name); 

Now suppose you want MAX_CUSTOMERS to be defined as 100. You can say:

 constant(MAX_CUSTOMERS) 

in code. In the get_constant code, you might have:

 int get_constant(const char *name) { if (!strcmp(name, "MAX_CUSTOMERS")) return 100; //shouldn't happen return -1; } 
0
source share

All Articles