Proper refactoring C

I have the following files: ah , ac , b1.c , b2.c and in both b1 and b2 I have some macro definitions that are identical.

Well, if I move them to ah or is it more common in the file, where are they used? How is this the right way to do this in C?

+4
source share
5 answers

Typically, macro definitions that are shared between files are moved to a header that includes both of these files. You must make sure that the file you are moving this definition to is a reasonable file to locate it; do not move it to a completely unrelated header just because it is included in both files. If there is no logical linked header file for b1.c and b2.c , perhaps you should create bh for sharing between them.

+8
source

I would say that there is nothing wrong with moving a macro to ah if it is included in both code files, provided that it makes sense for all files to include ah

+1
source

No matter what your boat floats.

If ah has anything to do with this macro, then this is a great place. If it is placed there "just because" ah may already be included in b1.c and b2.c, this is not a design problem, but a "convenience" problem. This is probably better than duplication, but ideally, if it is not related to ah, perhaps you can put it in bh (as it is related to b) or handymacros.h (since it is not very specific, but a does not use his).

+1
source

If the macro definitions do not stomp on the valuable namespace, insert them into the header that is most appropriate. If it is ah, go for it. It looks like he will most likely be bh

+1
source

If b1 and b2 actually refer to the same thing (if you change it, you have to change it in another), then the header is the right place to place these definitions. Then you only need to change it in one place and cannot accidentally forget the place to change it.

+1
source

All Articles