[CSC 315] When to Segment Source

Andrew J. Pounds pounds at theochem.mercer.edu
Thu Oct 7 07:52:29 EDT 2010


I had a good question asked of me the other day regarding when to start 
to segment your source code into multiple files.  Some folks are really 
hardcore about this and say that you should start the project this way.  
I find that a bit difficult and extreme when I am starting a project for 
the first time.   Here is what I recommend.

Get the most basic thing working.  In the case of your arrow code, that 
would mean getting the arrow on the screen, maybe one transformation, 
one keyboard interaction, and the pieces needed to get the animation 
moving.  Using OpenGL, that should be somewhere between 10 and 20 
functions when you include all the things like matrix multiplication, 
myinit, etc.  Anyway, get this working.

Once you have a working piece of code, copy it somewhere for safekeeping 
and then start to break your code into logical segments (you don't have 
to put one function per file, although that is a method that many people 
prefer).  In graphics work I prefer to group things together.  For 
example, I put all of my routines to build matrix transformations and 
apply matrix transformations in one file.  The only drawback to this is 
that you have to remember where you put the things (or rely on ctags to 
remember for you!).  You also have to remember that each time you make a 
new file you have to think about what its dependencies are and put the 
proper include statements at the top of the file.

Anyway, get your simple working source segmented, and then work on 
creating the makefile for it.  The goal is to get everything work just 
as it did in a combined source.  This breaking up process, including the 
building and testing of the makefile, takes less than an hour in most 
cases, but if your combined source has many functions it could take 
longer.  I think it took me about 20 minutes to break up the "movedot" 
program that I handed out in class.  Once you get that working, you are 
then in a position to start adding pieces -- but you now know EXACTLY 
how everything fits together and what dependencies each piece of code has.

One of the major benefits of doing this is that the next time you start 
writing a graphics application, you can simply pull in the routines you 
wrote in previous assignments.  As your list of routines grows, you can 
even build them into a library that you no longer have to compile -- but 
simply draw in at link time. Warning -- this assumes that the code in 
your library is PERFECT -- so definitely don't try that on a first go 
around...    What you should notice is that when you start future 
assignments you will be in a better position to begin them using a 
segmented model because you already have many of the major pieces in 
this form.


-- 
Andrew J. Pounds, Ph.D.  (pounds at theochem.mercer.edu)
Associate Professor of Chemistry and Computer Science
Mercer University,  Macon, GA 31207   (478) 301-5627



More information about the csc315 mailing list