Code organisation

Yello, I always try to oraganise my code into functions just to make it look better and have main() be much smaller. I thought it would make things easier to handle but I get lots of problems with it, usually to do with either having to pass loads of variables to nearly every function, or making everything global. For instance with a game I'll have one function for input, another for mechanics, another for graphical output etc.
So, am I just making things hard for myself, or am I doing it wrong? Is this commonly done? I rarely see it like that in other people's code, and when I do it's pretty complex and I can hardly understand it.
Thanks.
It may be that your understanding of what should be in functions and what should be in main is where the problem is. Can you post an example of your code for us to look at?
It could also be that you're passing around variables that should be inside structures.
You know, I think I've just gotten ahead of myself. It's not like I'm writing millions of lines of code. I'll reorganise it so it's all in main() until I've finished a decent project and actually know what I'm talking about. Tetris here I come!
no, no, no! good code organization is essential even in small programs. It's better to learn how to divide a program down into functions before you start attempting large projects. We just want to see your code so we can understand what you're dealing with.
Yello, I always try to oraganise my code into functions just to make it look better and have main() be much smaller.


As you should.

I thought it would make things easier to handle but I get lots of problems with it, usually to do with either having to pass loads of variables to nearly every function, or making everything global.


If you are developing in pure-C++ you should be looking at how to build Object-Orientated applications. Nothing should be global.

For instance with a game I'll have one function for input, another for mechanics, another for graphical output etc.


hmmm. Newbies fascination with writing games. Games are extremely complicated systems. You don't simply have 1 function for "input". You have an entire class devoted too Mouse input. Another class for keyboard, another for joystick, another for network. Mechanics, a class for physics control, another for each of sound, textures, loading, players, ai, rendering etc.


IMO. Read some books and seriously take the time to study Object-Orientated development. Then you might understand how to split your code up into more manageable pieces.
Topic archived. No new replies allowed.