How to better manage large .cpp files

This is a discussion on How to better manage large .cpp files within the C++ Programming forums, part of the General Programming Boards category; Hey. I have a .cpp that has 2000 lines of code and it's probably going to get bigger. The project ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217

    How to better manage large .cpp files

    Hey. I have a .cpp that has 2000 lines of code and it's probably going to get bigger. The project is a game server and the file is for the main "Server" class that processes all the messages from the clients. Is it normal for files to get this large, and how would large, commercial games deal with this?

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    The only thing I can suggest is splitting some of the function prototypes into a seperate header file. If you have the class definition in the .cpp file put that in its own header to reduce the code a bit.

    Having large source files is ok if you do need the code to work together function by function. Like above, you could also maybe take a look at your code and decide if some of it could sit in it's own file and then link the files together via header files which hold their prototypes.

    It is just one idea, I am sure others will give you a better one mind. Best of luck.
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    Can you categorize the code further than just what it currently is? Even if it's named "Server," a server does a lot of things, so you could probably categorize it according to that and move code out of the file that way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First of all, a 2000 line source file isn't particularly large in my mind. No, it's not small, but it's not HUGE either. I've worked on files that are several times bigger. I think about 20K lines is the largest file I've ever worked on (but I'm sure there are larger ones out there).

    I'm not saying that it's a good idea to have very large files, but sometimes there just isn't any good place to split the file up - and it's no point really in splitting the file SIMPLY because it is large. Putting stuff in a header file that is only included in one place is no solution - that just makes stuff HARDER to find.

    On the third side of this, we have the fact that your "server class" is 2000 lines - that is pretty large for a single class. It probably matches the "swiss army knife"/"God object" anti-pattern: http://en.wikipedia.org/wiki/Anti-pattern

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Quote Originally Posted by matsp View Post
    First of all, a 2000 line source file isn't particularly large in my mind. No, it's not small, but it's not HUGE either. I've worked on files that are several times bigger. I think about 20K lines is the largest file I've ever worked on (but I'm sure there are larger ones out there).

    I'm not saying that it's a good idea to have very large files, but sometimes there just isn't any good place to split the file up - and it's no point really in splitting the file SIMPLY because it is large. Putting stuff in a header file that is only included in one place is no solution - that just makes stuff HARDER to find.

    On the third side of this, we have the fact that your "server class" is 2000 lines - that is pretty large for a single class. It probably matches the "swiss army knife"/"God object" anti-pattern: http://en.wikipedia.org/wiki/Anti-pattern

    --
    Mats
    Well the class originally was like a "god object" but i moved alot of the methods into their own class like instead of "Server::FindFile()" i made a class called "FileManager" which could be used to do that and more (mainly because it didn't seem right to pass a reference to Server to other classes that need to find a file). The reason it's so large is because it processes all the player packets. Heres most of the methods for Server:

    Code:
    bool msgLOGINPACKET(Player*, ByteStream&);
    	void msgLEVELWARP(Player*, ByteStream&);
    	void msgBOARDMODIFY(Player*, ByteStream&);
    	void msgPLAYERPROPS(Player*, ByteStream&);
    	void msgNPCPROPS(Player*, ByteStream&);
    	void msgADDBOMB(Player*, ByteStream&);
    	void msgDELBOMB(Player*, ByteStream&);
    	void msgTOALLMSG(Player*, ByteStream&);
    	void msgADDHORSE(Player*, ByteStream&);
    	void msgDELHORSE(Player*, ByteStream&);
    	void msgADDARROW(Player*, ByteStream&);
    	void msgFIRESPY(Player*, ByteStream&);
    	void msgCARRYTHROWN(Player*, ByteStream&);
    	void msgADDEXTRA(Player*, ByteStream&);
    	void msgDELEXTRA(Player*, ByteStream&);
    	void msgCLAIMPKER(Player*, ByteStream&);
    	void msgBADDYPROPS(Player*, ByteStream&);
    	void msgHURTBADDY(Player*, ByteStream&);
    	void msgADDBADDY(Player*, ByteStream&);
    	void msgSETFLAG(Player*, ByteStream&);
    	void msgUNSETFLAG(Player*, ByteStream&);
    	void msgOPENCHEST(Player*, ByteStream&);
    	void msgADDNPC(Player*, ByteStream&);
    	void msgDELNPC(Player*, ByteStream&);
    	void msgWANTFILE(Player*, ByteStream&);
    	void msgNPCWEAPONIMG(Player*, ByteStream&);
    	void msgEMPTY25(Player*, ByteStream&);
    	void msgHURTPLAYER(Player*, ByteStream&);
    	void msgEXPLOSION(Player*, ByteStream&);
    	void msgPRIVMESSAGE(Player*, ByteStream&);
    	void msgDELNPCWEAPON(Player*, ByteStream&);
    	void msgMODLEVELWARP(Player*, ByteStream&);
    	void msgPACKCOUNT(Player*, ByteStream&);
    	void msgTAKEEXTRA(Player*, ByteStream&);
    	void msgADDWEAPON2(Player*, ByteStream&);
    	void msgUPDATEFILE(Player*, ByteStream&);
    	void msgOUTTERMAP(Player*, ByteStream&);
    	void msgEMPTY36(Player*, ByteStream&);
    	void msgLANGUAGE(Player*, ByteStream&);
    	void msgTRIGGERACTION(Player*, ByteStream&);
    	void msgEMPTY39(Player*, ByteStream&);
    	void msgCSHOOT(Player*, ByteStream&);
    	void msgEMPTY41(Player*, ByteStream&);
    	void msgEMPTY42(Player*, ByteStream&);
    	void msgEMPTY43(Player*, ByteStream&);
    	void msgEMPTY44(Player*, ByteStream&);
    	void msgEMPTY45(Player*, ByteStream&);
    	void msgEMPTY46(Player*, ByteStream&);
    	void msgEMPTY47(Player*, ByteStream&);
    	void msgEMPTY48(Player*, ByteStream&);
    	void msgEMPTY49(Player*, ByteStream&);
    	void msgEMPTY50(Player*, ByteStream&);
    	void msgSWANTSOPTIONS(Player*, ByteStream&);
    	void msgSSETOPTIONS(Player*, ByteStream&);
    	void msgWANTRCFOLDERS(Player*, ByteStream&);
    	void msgSETRCFOLDERS(Player*, ByteStream&);
    	void msgSETRESPAWN(Player*, ByteStream&);
    	void msgSETHORSELIFE(Player*, ByteStream&);
    	void msgSETAPINCRTIME(Player*, ByteStream&);
    	void msgSETBADDYRESP(Player*, ByteStream&);
    	void msgWANTPLPROPS(Player*, ByteStream&);
    	void msgSETPLPROPS(Player*, ByteStream&);
    	void msgDISPLAYER(Player*, ByteStream&);
    	void msgUPDLEVELS(Player*, ByteStream&);
    	void msgADMINMSG(Player*, ByteStream&);
    	void msgPRIVADMINMSG(Player*, ByteStream&);
    	void msgLISTRCS(Player*, ByteStream&);
    	void msgDISCONNECTRC(Player*, ByteStream&);
    	void msgAPPLYREASON(Player*, ByteStream&);
    	void msgLISTSFLAGS(Player*, ByteStream&);
    	void msgSETSFLAGS(Player*, ByteStream&);
    	void msgDADDACCOUNT(Player*, ByteStream&);
    	void msgDDELACCOUNT(Player*, ByteStream&);
    	void msgDWANTACCLIST(Player*, ByteStream&);
    	void msgDWANTPLPROPS(Player*, ByteStream&);
    	void msgDWANTACCPLPROPS(Player*, ByteStream&);
    	void msgDRESETPLPROPS(Player*, ByteStream&);
    	void msgDSETACCPLPROPS(Player*, ByteStream&);
    	void msgDWANTACCOUNT(Player*, ByteStream&);
    	void msgDSETACCOUNT(Player*, ByteStream&);
    	void msgDRCCHAT(Player*, ByteStream&);
    	void msgDGETPROFILE(Player*, ByteStream&);
    	void msgDSETPROFILE(Player*, ByteStream&);
    	void msgWARPPLAYER(Player*, ByteStream&);
    	void msgDWANTRIGHTS(Player*, ByteStream&);
    	void msgDSETRIGHTS(Player*, ByteStream&);
    	void msgDWANTCOM(Player*, ByteStream&);
    	void msgDSETCOM(Player*, ByteStream&);
    	void msgDEDITBAN(Player*, ByteStream&);
    	void msgDSETBAN(Player*, ByteStream&);
    	void msgDWANTFTP(Player*, ByteStream&);
    	void msgDCHANGEFTP(Player*, ByteStream&);
    	void msgDENDFTP(Player*, ByteStream&);
    	void msgDFILEFTPDOWN(Player*, ByteStream&);
    	void msgDFILEFTPUP(Player*, ByteStream&);
    Last edited by 39ster; 08-25-2008 at 06:16 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Surely all those packets can be made into abstract objects, and let the server handle the generic objects, rather than having 100 different object types and a function for each?

    That would of course create a new message.cpp functionality, but that would be a lot of small classes, rather than one huge one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yes, this looks very much like a place for the Command pattern.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help searching words in large files
    By nick2 in forum C Programming
    Replies: 5
    Last Post: 05-30-2009, 03:51 AM
  2. Get .cpp and .h files to communicate...
    By yaya in forum C++ Programming
    Replies: 6
    Last Post: 11-25-2008, 12:45 AM
  3. query regarding transferring large files across network
    By arron in forum Networking/Device Communication
    Replies: 3
    Last Post: 09-12-2005, 11:53 AM
  4. Class files (.h and .cpp
    By Todd in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 03:07 PM
  5. Reading Large Files!!!
    By jon in forum Windows Programming
    Replies: 1
    Last Post: 09-10-2001, 12:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21