1

As I've learnt more C++ with time, I've looked for a use for multiple .cpp files in one solution. This has been to no avail.

From what I can tell, multiple .cpp files just add more complexities when it comes to making sure there are no duplicates of integers or whatever, and all the idea is used for is just to sort an application a bit better. Is that the only use of doing this?

  • See: https://stackoverflow.com/questions/21898770/file-scope-and-global-scope-c-c – Michael Anderson May 07 '19 at 05:12
  • 1
    Better code organization, data isolation / variable scoping, keeping memory/resource usage in check when compiling and linking growing amounts of code, etc etc etc – Remy Lebeau May 07 '19 at 05:23
  • Remy- I see now. Thanks for the heads up. – kineticcrusher May 07 '19 at 05:24
  • Dividing a growing project into multiple files is IMHO the main purpose. Every serious programming language I know supports this. In C++, a `.cpp` file is usually compiled on its own. That makes it a Translation unit: [Global namespace scope](https://en.cppreference.com/w/cpp/language/scope#Namespace_scope). There once was a nice Q/A [SO: What is a "translation unit" in C++](https://stackoverflow.com/q/1106149/7478597) which might help further. – Scheff's Cat May 07 '19 at 05:24
  • Concerning _a .cpp file is usually compiled on its own._ It is not ... forbidden to `#include` `.cpp` files in other `.cpp` files. It's rather "against the usual convention" and might be confusing for (other) readers. An `#include`d file is not a translation unit but only the files passed to the compiler directly. – Scheff's Cat May 07 '19 at 05:27
  • It's about managing complexity. If you have small set of related classes and functions in a 100M LOC project, then putting them in their own files allows you to prevent other code from polluting their functionality. And facilitates testing them in isolation. – StoryTeller - Unslander Monica May 07 '19 at 05:33
  • Btw. I just remembered that we once got an error in VS because there were too much methods in one class. -> The allowed code size for one translation unit was exceeded. (Sounds like a bad design (and it even might've been) but it was the main application class with a lot of user commands...) We had to split the implementation of that class into multiple files to get this compiled. – Scheff's Cat May 07 '19 at 07:18

1 Answers1

3

As a beginner even I thought that multiple files made it difficult for me to understand the code. But after working on a few projects I realized the importance of this modularity approach. There are many reasons for having multiple files in the project. Here I list some of them.

Let's say you are in company and you want to distribute task between many employees. Now if you as them to work on a single file then it will get difficult to keep track of who is editing what. So, we break down the task in smaller tasks where each task can be done by an individual. We provide him with a particular interface. In the case of C++, we can provide a header file with public methods and public variable. So now this individual knows what methods he can use from the part that his coworkers are working on.

So, in this situation, multiple files serve the following functions:

  1. Divide and Conquer: Divide task between co-workers without running into the risk that they might break someone else's code.
  2. Keep code modular, where each file/class has a specific function. So, that in future if we want to add or delete some features then we can just edit that class without tinkering too much with entire codebase.
  3. Optimize compilation: Let's say you have 400 files in code and now you change 1 line in a certain file. Now you can individually compile the edited file. Wherelse if it was one giant file then it will waste time to compile things that are not changed.

About complexity, actually distributing code in different files reduce code complexity.

Consider that you are working on a solo project. Let's say it has some database, functionality, input-output, GUI, some calculations like searching, etc. Now consider if you include all this thing in 1 file then it will become difficult to keep track of various thing in no time as project size increases where fast without even creator realizing. This will lead to duplicate names of functions, variables, structures, etc. Now instead of having functions log in each file which logs the status of the database, input-output, etc of its respective file, you will have one file with databaseLog, inputLog, etc. And what's the guarantee that you will not have some other functionality in GUI for which inputLog will not be the suitable title.

Let's say you have some error or crashes in a project that it is easier to look at one file as there are fewer things to look at. While in one file you will have a difficult task in keeping track of what belongs to what functionality. So, Debugging gets simplified.

So, in short, you can say that if file names are assigned according to their purpose than multiple line code reduces code complexity rather than increasing it.

If you are trying to read someone else's code with multiple files then here are some tips,

  1. Look at one file at a time and find what exactly it is doing. Most beginners do mistake that they start with main and keep on searching functions as they encounter.
  2. I will recommend you to note down what each function do.
  3. Use flowcharts.

And when you are working on the project make sure that each file has its own purpose, it is modular and functions do one thing. Make sure files are in a proper hierarchy according to their purpose. Also, it will be easier for you to try and implement these practices in future if use them daily. Good luck.

Hope it helps.

Community
  • 1
  • 1
Mandar Sadye
  • 689
  • 2
  • 9
  • 30