Search code examples
c++coding-styleconventions

Why is it recommended to use multiple files when making a C++ project?


I'm Learning C++ and coming from GML which is pretty similar in syntax. But what I don't understand is why it's recommended to break up a project into multiple files and multiple types of files.

From what I understand, cpp files are the actual program code so people use multiple to break up the functions and parts of the program into seperate files. Header files are the same code as cpp but they're used by cpp files so it's repeated code that can be referenced in multiple places. Is this correct?

To me it doesn't make as much sense because you're not gonna be swapping out files for different builds, you still have to recompile and all the files get merged into a binary (or a few if there's dlls).

So to me, wouldn't it make more sense to just have one large file? Instead of having header files that have repeated references to them in many cpp files, just have the header files code at the top of a single cpp file and create sections down the file using regions for the content of what would be many cpp files.

I've seen a few tutorials that make small games like snake using a single file and they just create sections as they move down. First initializing variables, then another section with all the functions and logic. Then a renderer then at the bottom the main function. Couldn't this be scaled up for a large project? Is it just for organization because, while I'm still learning, I feel it's more confusing to search through many files trying to track what files reference which other files and where things are happening. Vs if it was all in one file you could just scroll down and any references that are made would be to code defined above.

Is this wrong or just not common? Are there drawbacks?

If someone could shed some insight I'd appreciate it.


Solution

  • You can use one file if you like. Multiple files have benefits.

    • Yes, you do swap different files in and out for different builds. Or, at least, some people do. This is a common technique for making projects that run on multiple platforms.

    • It is hard to navigate a very large file. Projects can have 100,000 lines of code, or 2,000,000 lines of code, and if you put everything in one file, it may be extremely difficult to edit. Your text editor may slow to a crawl, or it may even be unable to load the entire file into memory.

    • It is faster to build a project incrementally. C++ suffers from relatively long build times, on average, for typical projects. If you have multiple files, you can build incrementally. This is often faster, since you only have to recompile the files that changed and their dependencies.

    • If your project is extremely large, and you put everything in one file, it’s possible that the compiler will run out of memory compiling it.

    • You can make unnamed namespaces and static variables / static functions, which cannot be called from other files. This lets you write code which is private to one file, and prevents you from accidentally calling it or accessing the variables from other files.

    • Multiple team members can each work on different files, and you will not get merge conflicts when you both push your changes to a shared repository. If you both work on the same file, you are more likely to get merge conflicts.

    I feel it's more confusing to search through many files trying to track what files reference which other files and where things are happening.

    If you have a good editor or IDE, you can just click on a function call or press F12 (or some other key) and it will jump to the definition of that function (or type).