Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opinions regarding C++ programming practice

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!

like image 556
Sagar Avatar asked Apr 19 '10 14:04

Sagar


4 Answers

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...)

like image 145
Péter Török Avatar answered Nov 14 '22 04:11

Péter Török


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.

like image 41
Jonathan Swinney Avatar answered Nov 14 '22 05:11

Jonathan Swinney


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.

like image 40
Fabio Ceconello Avatar answered Nov 14 '22 04:11

Fabio Ceconello


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.

like image 44
Goz Avatar answered Nov 14 '22 04:11

Goz