Is there a reason for doing this | Sololearn: Learn to code for FREE!

+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???

1/6/2020 8:37:17 AM

IDGam3r

10 Answers

New Answer

+9

Writing everything in a single file is a bad idea. The size of source file would be very big and big files takes longer to compile. Loading large files in the editor itself is an issue. In the event of some failure you are likely to use your whole code in one shot, sure you may use some version control, the risks are still high. You loose code reusability, what do you do if you want to use certain parts of code in another project? Copy-paste is error prone. You loose code modularity, since everything is in same file. Testing would be a real pain, you will have hard time testing the features, and can't do independent testing. Smallest change would require full recompilation. These are just some of the issue you will have if everything is in same file.

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

+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?

+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 :)

+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 : )

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

+3

HonFu Follow stong cohesion and low coupling rule i.e keep the related things together, yet there should be minumum dependencies. Getting it right take's considerable experience.

+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"

+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

0

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