Is there a reason for doing this | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is there a reason for doing this

So I was going through the Source code for Quake, Quake 2, Quake 3, etc and I noticed that everything had its own " Header" and class files, for example the rocket launcher has its own class and header file, the main character has its own class and header file, the actuall gameplay mechanics have their own class and header file, forgive my primitive, poorly educated brain, but is there a reason WHY they do that, would'nt it be easier to "clump" it all into a single file???

6th Jan 2020, 8:37 AM
IDGam3r - avatar
8 Answers
+ 6
In general, separating a project into multiple files makes it easier to debug, update and maintain. If you want to change the code for the rocket launcher for example, you know which file to open and update. No need to scroll through piles of lines like you would; should you choose to cram the whole thing into a single file.
6th Jan 2020, 10:25 AM
+ 5
Although file separation sounds logical in general, I am myself wondering where to conveniently draw the line. You don't want a million lines of code in one file, but storing each 10-liner free function in a separate file also won't necessarily make maintenance easier, right? Because you'll have folders upon folders stuffed with mini-files. Are there some guidelines about what or how much to put together and where to place the cut?
6th Jan 2020, 10:42 AM
HonFu - avatar
+ 5
Regarding the level at which to separate code, the general guidelines we follow at my company are to put every class declaration in a header file, and the member function definitions in a corresponding cpp file (sometimes just in the header if a one-liner). The files are named with a combination of the namespace name and class name, so locating classes is very easy - much easier than searching through a giant header file! As ~swim~ alluded to, sometimes getting the balance right takes experience. I once had to move an enum into its own file to avoid introducing an undesirable dependency between two libraries - since then I've been in the habit of putting enums in their own files on the basis that it's not much effort, and you never know when you might regret not doing so. Good habits, even when seemingly overkill, pay dividends :)
6th Jan 2020, 10:18 PM
Simon Castro
Simon Castro - avatar
+ 4
HonFu I wonder the same question also. But IMHO modular approach is inevitable for big projects. I'm all ears about the guidelines for when to unite or separate too : )
6th Jan 2020, 10:55 AM
+ 3
I heard stories where it took a week to recompile CAD. Imagine doing that every time you make even 1 change in your 1 file project. No thanks.
6th Jan 2020, 9:41 AM
Dennis - avatar
+ 2
JAAI I really appreciate the kind words sir, I looked into the games source code out of curiousity, I was Impressed and onfused and didnt understand 90% of what was written, such as to pull say 4 lines of code from a Quake II Source Code(which is Coded in C, Which is why I am trying to learn and understand C before touching on C++ or C#)Class is: static menuframework_s s_multiplayer_menu; static menuaction_s s_join_network_server_action; static menuaction_s s_start_network_server_action; static menuaction_s s_player_setup_action; Like this and other parts of the source code of Quake II both intrigue me and confuse me, the intrigue is why I go "looking under the hood"
7th Jan 2020, 9:56 AM
IDGam3r - avatar
+ 1
Also IDGam3r you aren't poorly or primitive minded.... if that had been the case you might have not dared into viewing the source code for a GAME which itself is a inquisitively daring thing to do
7th Jan 2020, 8:54 AM
Aditya - avatar
nope it ain't that easy to compile every single line of code into one single file. Whenever you complete a Game subtitles are shown those are the persons who actually worked on that have to make the game successful..... So it comes to the conclusion every single person must be responsible for a part of game development or the other..... hence when one group of people works for character designing other group focuses on character and that's the reason the complete source code of game consists of various header files and class files respectively
7th Jan 2020, 8:53 AM
Aditya - avatar