Organization of a program

closed account (4Gb4jE8b)
Is it better to have multiple thousand lines of code directly in int main() on a console program, or is it better to have multiple functions accurately breaking up those thousand lines of code? What are your guys' personal opinions?

*note this question has come about because i'm dealing with 200+ lines of source code all directly in int main(). The reason i ask for thousands is because taking it to a more realistic commercial level represents my future :P
closed account (3hM2Nwbp)
If you can break the program down into subroutines that make sense, there's no reason not to. Better yet, if you can break down the program into separate parts (classes) that accomplish singular tasks that work together with each-other there's no reason not to.
Have you begin working with classes yet? What does your program do?

I made a program that converted to and from morse code. I had one main function, then I had a function for converting to morse, a function to convert from morse, and then a function to call a help menu. The help function itself called for two additional functions. That's how I split it up.
closed account (4Gb4jE8b)
well my program goes ahead and splits up text files into 4kb (though that's going to change to a user entered number soon).

and no, i haven't worked with classes yet, and when i have tried, i really didn't understand what i was doing. if you can point me to some good readings/tutorials it would be appreciated, i tried using the ones here and it didn't really help me.

But yeah, i was just asking what you guys do, so thanks!
closed account (3hM2Nwbp)
So your program takes a large text file and breaks it down into smaller files of a user defined size? If that's the case, then you're probably breaking off parts of the file into byte arrays to write to the other files. You could make a saving routine that takes this form:

1
2
3
4
5
6
7
8
///Writes the specified data to a file
/**
 * @param fileName - The file to write to
 * @param data - The data to write
 * @param length - The length of the data
 * @return - Did the file write correctly?
 */
bool saveTo(const char* fileName, char* data, unsigned length);


I'm not sure if that's what you need, but just food for thought
I'd imagine splitting it up would make it run a little slower, however if you don't split it up, and make an error (almost guaranteed to happen in such a large program) it will be MUCH more difficult to debug.
Splitting cause slowness? Only if your header doesn't have inlines for the appropriate functions.
Last edited on
Topic archived. No new replies allowed.