aestond wrote:
this time a question, probably verify me as a total beginner*g*
Verified! :)
It is not a problem at all ;)
aestond wrote:
The easiest way for me to handle all of them is
initialising them as globals.
I can edit and access them from everywhere,
no need to give them as a parameter for a function.
Con: It is not protected, can be changed from
everywhere, but I don't know why it is bad*g*
The name of the global variable appears in the function code. That means if you have two variables that need the same kind of operation, you need to duplicate the function.
Duplicate code is bad, as one day you will find a bug, fix one, and forget you had a second one.
This is the basic reason why you want to use parameters, you want to avoid duplicate code at nearly all costs, as it will kill you (it eats development time by the months).
aestond wrote:
Secons idea is, that initialize them in the main loop
and use them as parameters for function always.
Arrays could be given as pointers.
Con: I think the chance to make syntax and logical errors is very high.
There is not much difference between a global and a main function variable. It costs the same amount of memory in both cases.
Passing variables through parameters has the advantage that the function does not care where the variable comes from, so you can avoid the problem I sketched above.
You can of course also pass global variables as parameter.
aestond wrote:
Third idea is making a class, holding all needed vars.
Making an instance and only use the pointer to the instance.
It's a kind of 'walking' global variables. Not useful imho.
aestond wrote:
I hope you my thinking is rethinkable(is that a real word?*g*)
and you can help me with some tips :)
The word is 'reconstructable' I think, and yes, your ideas are clear to me (or at least I think they are :p )
Programming is all about structure. You are handling a very complicated problem, so you need all the help you can get.
Creating structure in data is quite powerful, and that is what I am missing in your list (that is, all your solutions are 'one long list' where the place just varies a bit).
One long list of variables soon becomes unmanagable no matter where you put it. Merging 'related' data in a class is a useful solution, ie instead of
Code:
int mario_xpos;
int mario_ypos;
do
Code:
class Mario {
public:
int xpos;
int ypos;
}
Mario mario;
it adds a level of structure to your data, and thus reduces the number of variables at all levels, and reduces the number of parameters you need to pass as well.
In other words, making structure in your data is more important than the place where you keep the variables.
In general, you should end up with a small number of global variables (or in main).
There is not a single rule that fits all cases though.
I tend to make a global variable for each part, one for the display, one for graphics, one for the game world, one for 'things that move', etc.
I am not sure it is the right way; I do things differently at work, and I see other people also making other choices.
I can only say, just do wihat you believe is right, and don't be afraid to experiment or change it, if you think it will be better.
I have been programming many years, and I still change how I program every now and then (although the changes are quite small usually).
_________________
My project: Messing about in
FreeRCT,
dev blog, and IRC #freerct at oftc.net