Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Extra source/header files vs. 1 long source file

Author
Message
heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 25th Jul 2009 06:18
Hi! Everything I've done using Dark GDK has been done using just one long Main.cpp. So what are the advantages to having a bunch of header files and source files (for variables, functions, etc.) instead of one long Main.cpp file?

Use Google first... it's not rocket surgery!
Mista Wilson
16
Years of Service
User Offline
Joined: 27th Aug 2008
Location: Brisbane, Australia
Posted: 25th Jul 2009 06:25
The advantage isnt readily apparent until you get into programs that number many thousands or more lines of code.

It quickly becomes a pain in the rear to navigate up and down a 12000 line file looking for variables or whatever...

While there is no reason you cant have it all in one file if that suits you, it generally makes it easier both on you, and anybody else reading your code if its split up into various files representing your program's component parts.. for example, you might put everything to do with drawing your user interface into their own set of files named something like "UIStuff.h" & "UIStuff.cpp" and so on for the rest of your program.

For a good example of doing this to a working game, check out the the Dark Invaders tutorial.

If it ain't broke.... DONT FIX IT !!!
Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 25th Jul 2009 06:26
It just comes down to preference. I prefer sorting my big, important classes into their own .h and .cpp files to easily find and change their members easily if needed. It's easier than sorting through one file to find everything, even though you can make it neat with extra spaces, carriage returns etc. If your whole project is considerably large in terms of lines of code, then having everything in one file may be a disadvantage. But that's just my opinion.

Your_Health = (My_Mood == HAPPY) ? 100 : NULL;
heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 25th Jul 2009 06:55
Ok thanks! I guess I'll just keep it all as one file.

Use Google first... it's not rocket surgery!
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 25th Jul 2009 06:58 Edited at: 25th Jul 2009 07:00
Let's not forget the re-usability advantages too. If you develop a class that might find itself applicable in another program it's more convenient to include the .h and .cpp files as part of the project rather than doing a copy from a larger source file and pasting it into your current program.

EDIT: A bit more portable too to be able to keep all your classes in a folder that you can move between projects and locations if, like me, you're inclined to do some programming at home and some at work.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Mista Wilson
16
Years of Service
User Offline
Joined: 27th Aug 2008
Location: Brisbane, Australia
Posted: 25th Jul 2009 10:09 Edited at: 25th Jul 2009 10:12
Actually, Lilith's point is, I think, more important than the readability of a program.

If you end up working on a number of projects, you will probably write over time small functions and libraries of functions to automate simple tasks to save re-writing code all the time. If you have these in their own files, it will definately make it easier to take them between projects without alot of copying and pasting, it would also allow you to have a central library that you could include into your programs, so any changes made to it while your working on one project, can be reflected in any other project it is used in without manually copying and pasting bits and pieces.

EDIT : A simple example of this is a function to return an unused object ID. You could have that function re-written in every project that you work on, or you could have it in a single file on its own and include it into each of your projects, saving having to re-write it every time, and allowing you to improve it from one project and have the improved code reflected in all the projects that include it.

If it ain't broke.... DONT FIX IT !!!
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 25th Jul 2009 12:10
Organizing the code into several files becomes important when the code is getting big, but I like to chop it up even in smaller projects. It's good to place things that logically belong together (like one class, or the menu system) into a separate file. Then I can work on it, finish that part, and then forget about it and move on to another part of the program. The modules which are ready don't even need to be open in the editor. This makes it easier to find something and concentrate on the part you're working with.

Re-usability is a very good point as well, and let's not forget compile time either. Although it's not a big issue with today's fast computers, but still, if you change something, the compiler doesn't need to recompile the whole code, only the file that's changed. This makes starting up the program faster, which is good for testing when you change and start it many times.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 25th Jul 2009 12:55
Why did it take so many posts to mention the compile time speed gains? If you split your files up so that headers only declare and source files only define and when adding new classes you try to declare all the methods you'll ever need then you'll really cut down future compile times when you begin implementing your methods.

Unless you're using a supercomputer, compile times for projects with tens of thousands of lines will still take quite a while, of course it totally depends on what your code contains. However, splitting it up like this means editing any source file only requires compilation of that object and linking, if you edit the header then you'll also need to recompile anything including it which wastes a lot of time if that wasn't required.

I love organizing my code and especially my include files; while I don't quite go to extreme lengths and reserve each source+header pair to a single class I always group my headers into logical partitions like, Main/Scene/Character/Weapons/Items/etc, if any of those sections are really large, like Scene, then I may well split it up into several header/source combos, again for each major aspect of it. Doing so just makes the code so much neater to manage and I can't picture myself using a single source file for a project, unless it's incredibly short.

heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 25th Jul 2009 18:44 Edited at: 25th Jul 2009 19:31
On question... How do I make a class or struct so that I can use it in multiple source codes? (like using extern int, extern float, etc.)

EDIT: Nevermind, after a lot of guess and checking I got it working.

Use Google first... it's not rocket surgery!
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 26th Jul 2009 12:47
I use a singleton-type thing, even if it does have a few drawbacks.

heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 26th Jul 2009 19:05
Yea, I decided that I'm gonna use one file for all the main programing, then another 2 files for loading levels (header and .cpp) and then one last header for variables.

Use Google first... it's not rocket surgery!
Red Eye
15
Years of Service
User Offline
Joined: 15th Oct 2008
Location:
Posted: 26th Jul 2009 22:38
... i also use one for all functions.

--- Have no fear of perfection - you'll never reach it. ---
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 27th Jul 2009 05:26
One thing to bear in mind, if you include a source file for a collection of functions you use, all of those functions will be included in the resulting application, whether you use them or not.

If you include a class definition in your source file it will not generate any code until you actually declare an instance of that class.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 27th Jul 2009 23:14
Ok thanks Lilith and Red Eye! I have another source for my common functions and then everything else is in with my main loop. (common functions as in distance formulas and such).

Use Google first... it's not rocket surgery!
wickedly kick it
18
Years of Service
User Offline
Joined: 13th Jul 2006
Location: Fort-worth Texas
Posted: 27th Jul 2009 23:40
i can not stress this enough for any language that having tons of extra source files is alot easier to edit up and reuse! So make 10-20 source files, it keeps your code reusable and your main.cpp clean.

heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 28th Jul 2009 00:10
Thanks Wickedly but I like to rewrite code from scratch because it gives me a chance to improve on what I did and learn new things to make it easier. But I will make files for things like formulas and such.

Use Google first... it's not rocket surgery!
Jumpster
16
Years of Service
User Offline
Joined: 7th Feb 2008
Location:
Posted: 28th Jul 2009 15:48
Quote: "One thing to bear in mind, if you include a source file for a collection of functions you use, all of those functions will be included in the resulting application, whether you use them or not.
"


This is not neccessarily true. Now-adays, linkers are smart enough to know whether or not you are actually *using* a function that you declare (and implement). You may declare it in a header file, implement it in a source file but never *use* it (that is, call it) somewhere else... If you don't use it, it won't necessarily be included in the final executable (unless explicitly demanded - ie: /INCLUDE: linker command to "Force Symbol Reference")

Regards,
Jumpster

Login to post a reply

Server time is: 2024-10-01 08:37:55
Your offset time is: 2024-10-01 08:37:55