I’m trying to figure out if I understand correctly what modular code really is. I know that it’s extremely powerful / useful so I want to make sure I’m learning it correctly.
To start learning, I made a fairly simple system that groups Entities together based on some criteria. First come, first serve.
I also want to graphically display the groups and their member’s, to show the user (myself) what’s going on. I’ll use the SDL library for this.
So let’s divide that into some classes;
Entities
The main class “Entities” will have a Boost::mutex for thread safety, and a map holding pointers to actual entities. Let’s also make a function that retrieves a pointer to the actual entity from said map, when provided with the entity ID.
class Entities
{
public:
boost::mutex access_entitymap; //Thread safety
std::map<unsigned int, Entity*> entitymap;
Entity* getEntity(unsigned int entityID);
};
Entity
Holds information about the actual entity, such as it’s uniqueue ID. And a pointer to the parent “Entities”.
LFDGroups
The grouping system.
Includes a map with pointers to the actual groups,
a map with the group’s position in the queue (first come first serve. full groups are removed from this map to make iterating faster),
and a map with an entity’s position in the group (5 spots, which entity has taken which spot?).
Furthermore it also includes some functions to operate on said maps. Such as adding and removing members. The addmember() function will, as an example, make sure the entity is added to the group with the most priority, or that a new group is made if necessary.
groupObject
Holds information about the actual group, such as it’s uniqueue ID and it’s inhabitant entities’ IDs. But also has some functions such as;
LFDGroups *lfdgroup; //Parent LFDGroups;
bool isEmpty(); //is this group empty?
bool isFull(); //is this group full?
unsigned int getPosition(); //Position in queue?
bool addMember(unsigned int entityID,bool snatchIt);
/*Add a member. Calls removeMember() on the entities' groupObject
first if the entityID is already in another group.*/
bool removeMember(unsigned int entityID,bool snatchIt);
/*Remove a member from this group, and snatch a member from another
group further up in the queue. This ensures groups are always full
if enough people are queued.*/
bool hasMember(unsigned int entityID); //Is this entity ID in our group?
/*etc. etc.*/
Queue
Provides some ease-of-use functions to make testing easier. Such as;
void addFakeEntity(bool queuenow); //Create an all new entity and immediately queue it.
void removeFakeEntity(); //Remove a random entity from a random group
void removeFakeEntity(unsigned int entityID); //Remove a given entity from it's corresponding group.
SDLMain
Graphically displays what’s going on with LFDGroups’ groups. How many members it has, which spots are taken and which aren’t etc.
Also proves buttons to work with Queue’s functions. Such as the “Add” button will call “queue->addFakeEntity(true)”
So now I can just start the whole thing up easily;
#include "SDL_main.h" //gfx
#include "class_LFDGroups.h" //group system
#include "queue.h" //ease of use for testing
#include "class_entities.h" //entities
int main( int argc, char* args[] )
{
Entities * entities = new Entities;
LFDGroups * lfdgroups = new LFDGroups(entities);
Queue * queue = new Queue(lfdgroups);
SDLMain * sdlmain = new SDLMain(queue);
return 1;
}
- Entities can be used without anything else.
- LFDGroups can be used with nothing but entities.
- Queue needs both, but LFDGroups already contains a pointer to
Entities. After all, it can’t function without it. - SDLMain uses all 3.
And sure enough;

So this means that even if I do disable the graphically displaying of the groups, I can still use the actual grouping system for other things.
And also, functions relating to certain objects are within that object itself. As an example, the Entity* getEntity(unsigned int entityID); function is in the Entities class and not in groupObject, even though that is the only one that’s using it at the moment.
Is this all that modular coding is? Am I doing it right?
Yeah your program looks to adhere to modular programming paradigms, such as having separate components that come together to make up the program, and written in a way that particular components could be replaced.
So you’ve split the logic of the program into separate classes, and you also split the classes into separate compilation units (assuming so, based on the includes in your SDLMain.h source), so to add a new feature to say your group class, would only require changing that module (assuming the new function was only called by internally to that class), and from looking at your source the program is logically split into separate parts that are easily identifiable.
You also could easily replace part of your code, for example the GUI front end could be replaced with Qt or wxwindows for example. Since the operations the gui calls into the data don’t change, the new gui code can be written, and call the same set of operations, and the data components will not be altered.