Too many parameters! HELP!

Started by
9 comments, last by rip-off 11 years, 10 months ago
Hey! My first post here so tell me if i'm doing something wrong :P

I've started developing a game in C++. To begin with i just did some proof of concept with lots of (bad?) global variables.
After cleaning up and attempting to exhibit better programming practises i have been moving my variables into my main function and passing them to parameters. This is from my understanding the way to go since you have control of what functions can modify what variables.

The problem is that i'm passing way too many variables to functions that shouldnt need them.
An example is an objects move() function, this move function checks for collisions, if there is a collision it calls a damage function which might cause the player to die and then call a GameOver() function which resets the game.

The game over function needs to reset alot of variables which i as a (good?) c++ developer is passing to it as parameters, but those parameters come from the damage function which gets them from the move function. So when i call the move function in the main function, it basically needs pointers to all the variables in the game which seems really really silly!

I can't possibly believe this is the way to go, but neither is global variables.

So my question is if i should put these variables as static data members of a static class as a way to avoid global variables. But i've heard that this is just as bad as global variables as they suffer from the same problems (Who modified what?)

I've tried to find some high quality open source games on here, but every post with that purpose just tells the OP to go and get his hands dirty himself.

Any advice is appriciated! thanks!

EDIT:
tl;dr Should i use static members for game global variables, or if not, then what?
Advertisement
Consider defining classes and group the parameters into these classes. Try to keep parameters that belong together in the same class. Now you only need to pass around objects for the classes as arguments.

As a next step, consider making the functions you mentioned as member functions to the classes.

The design trick now is to define a good set of classes. It will take some practise to get right. Even experienced programmers have to redesign the class architecture now and then.

If you do this wrong, you put all global parameters into one class and make all functions member functions of this class. Then you are back to the original problem, but in an object oriented way.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/
It is good to use references for all side effects so that all input and output is explicit and the code can be reused in another context. If you have too many small variables, group them together using structs. If you refer to too many things that you don't need, only give references to a subset of the data.
Thanks alot, this seems to be what i need!
On the subject of references, i don't really see a good reason to use them. (I prefer pointers, const pointers etc)
But from what i've been able to read, thats a personal preference and i'm fairly experienced in C so. (C# also, so references aren't mysterious to me)
Sometimes you NEED to use pointers, other times you CAN use references, so i'd rather make my code more uniform than gain a little safety and 'easy on the eyes notation'.
Unless im missing something about references?
I agree with you about references. You can define arguments as references and then mutate the parameters in ways that are unexpected. References is usually only a syntactic sugaring.

There is one way I use references now and then: When I find optimization is needed and arguments are objects of a certain size. For example, sending a 4x4 transformation matrix as argument. In this case, declaring the argument as "const matrix4x4 &transform" will give the speed of a pointer with the syntax of a normal object. This can be done with pointers also (const matrix4x4 *transform), but then you have to change all calls to the function. And it gets messy if you want to use overloaded functions defined for the type (like projectionMatrix * viewMatrix * modelMatrix * vertex).
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/
Noted thanks :)
In many cases pointers and references can be interchanged, but not all. A pointer implies that the value may be null; a reference makes it explicit that null is an invalid value for the variable. Getter functions for classes are typically better implemented as returning a reference to const rather than returning by value. Pointers don't make sense in that context. And operator overloading requires the use of references. If you need to be able to change which object your variable dereferences to, you cannot use a reference, so a pointer is necessary.

References tend to result in cleaner-looking, easier to read code, which is important since coders tend to read code more than they write it. That benefit, small as it may seem, should not be ignored. I concede that in many cases it is more of a stylistic choice, but I encourage the approach of using references when possible and pointers only when one must.

I agree with you about references. You can define arguments as references and then mutate the parameters in ways that are unexpected. References is usually only a syntactic sugaring.


That's completely incorrect. The use of references, and even more importantly 'const', states a contract between the function and the caller that defines if and how the function may modify those parameters. Yes, it's often possible in C++ to wiggle out of those contracts by abusing casts, but just because no contract can force a party to act in good faith does not mean that the contract itself is not important. In C++ the contract is important because the compiler will help you to inadvertently violate it (violating the contract usually requires tricky syntactic gymnastics), and also helps the compiler to understand the code more-fully by precluding certain possibilities, the result of which is that the compiler can often make more-aggressive optimizations.


There is one way I use references now and then: When I find optimization is needed and arguments are objects of a certain size. For example, sending a 4x4 transformation matrix as argument. In this case, declaring the argument as "const matrix4x4 &transform" will give the speed of a pointer with the syntax of a normal object. This can be done with pointers also (const matrix4x4 *transform), but then you have to change all calls to the function. And it gets messy if you want to use overloaded functions defined for the type (like projectionMatrix * viewMatrix * modelMatrix * vertex).
[/quote]

References should always be preferred over pointers, except where pointers are necessary (dynamically-allocated memory or other late-binding, null values, pointer arithmetic, interacting with legacy interfaces that use pointers). When you do need a pointer, the standard library provides a number of smart-pointers that should be preferred over home-grown pointer classes (I refuse to call them "smart" pointers, as home-grown pointer classes are almost universally broken in ways both obvious and not) or raw pointers. With C++ 11, just about every need you'd have for a pointer, aside from ones to deal with legacy interfaces, is met by one of the pointers in the standard library.

'const' should be applied appropriately regardless of whether the parameter is passed by value, pointer, or reference.

throw table_exception("(? ???)? ? ???");

<br />When you do need a pointer, the standard library provides a number of smart-pointers that should be preferred over home-grown pointer classes (I refuse to call them "smart" pointers, as home-grown pointer classes are almost universally broken in ways both obvious and not) or raw pointers. With C++ 11, just about every need you'd have for a pointer, aside from ones to deal with legacy interfaces, is met by one of the pointers in the standard library.<br />


Additionally, even if C++11 isn't available, boost almost always is. Its smart pointers library is well tested and proven, and is almost completely compatible with the C++11 variants (seeing as how the C++11 variants are BASED on the boost ones).

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


[quote name='larspensjo' timestamp='1339095647' post='4947131']
...You can define arguments as references and then mutate the parameters in ways that are unexpected. References is usually only a syntactic sugaring.
That's completely incorrect.[/quote]
I think there is a misunderstanding, I didn't state the use of "const" in this example.

References should always be preferred over pointers, except where pointers are necessary (dynamically-allocated memory or other late-binding, null values, pointer arithmetic, interacting with legacy interfaces that use pointers).
[/quote]
One advantage is that it is difficult to make a reference that is a null pointer. That means the source code will become more resilient to errors.

The exception, where pointers can have null values, is quite a big exception. It is a common practice to return a 0 pointer to indicate either a failure or that there is no object associated with the pointer. I am not sure if smart pointers help with this?


'const' should be applied appropriately regardless of whether the parameter is passed by value, pointer, or reference.
[/quote]
Agreed.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

This topic is closed to new replies.

Advertisement