I have a program that I am writing, not too big. Apart from the main function, it has about 15 other functions that called for various tasks at various times. The code works just fine all in one file, and as it is right now.
However, I was wondering if anyone had any advice on whether it is smarter/more efficient/better programming to put those functions in a separate file different from where main is, or whether it even matters at all. If yes, why? If no, why not?
I am not new at C++, but definitely not an expert either, so if you think this question is stupid, feel free to tell me so.
Thanks for your time!
Depends on how big those functions are. If your source file starts to get over several hundred lines of code in length, there is reason to extract part of the functionality into one (or more) separate file(s).
If you can group the functions into distinct sets based on their responsibilities and/or their level of abstraction, you may prefer separating them into distinct physical files (and classes of course) along those lines. E.g. some functions may work with file I/O while others do some computation. Or some functions do low level bit flipping chores within file I/O, while others build on the former to implement some more abstract functionality.
Another reason to partition your code would be if some of the functions were used by more than one client, but this apparently does not apply to your case. (However, this is probably going to change if / as your app is further developed and extended in the future...)
It's good to separate your code into different files based on their similarity. You could break it up by class if you have multiple classes. If you have several functions that could be used in other programs, you should put them in their own file for portability.
Since you mentioned only functions, I assume your program is not object-oriented. If it were, I'd advice to have one class per .h/.cpp pair. In your case it depends on whether those functions can be grouped into 2 or more subsets. If so, I'd put related functions together in separated .cpp modules, and always having a corresponding .h header containing their prototypes.
Having all those 15 functions in the same module isn't necessarily correct or incorrect. Again, if they are all strongly related they should belong to the same module. Another rule to decide is the module size itself. I find hard to manage a module that has grown to more than 1000 lines, this threshold is a warning sign that it should be split. These two things are related, usually when the module gets this big it also probably has two or more distinct groups of functions.
Assuming that each function is pretty small then it doesn't make sense to split it between files, IMO.
If functions are several hundred lines of code then it might be better to split it out. Mainly because it make it easier if/when you extend the program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With