NinjaGameDev
NinjaGameDev

Reputation: 93

How to avoid too many arguments in a function. C++

I am making a small game in my small game engine made with OpenGL c++. I have to make many classes like Camera, BatchRenderer, AudioEngine, ParticleEngine, Level, Player, Zombie, Humans, etc. Now Player have to interact with classes like Camera, BatchRenderer, AudioEngine, ParticleEngine, level data from Level class, Zombie, Human, etc. And i am passing most of them by reference which are initialized in my Main Game class.

// AudioEngine and ParticleEngine which is created in Main Game class and Player need to play sound and simulate particles. TextureID, position, radius(size), rotation, depth and color are needed to render the player.

Player::Player(AudioEngine& a_audioEngine, ParticleEngine& a_particleEngine
               GLuint a_textureID, glm::vec2 a_position, float a_radius,
               float a_rotation, float a_depth, Color a_color)
{...}

// Camera for checking if the player is in view. levelData, zombies, humans to check collision detection with Player.

void Player::update(Camera& camera, std::vector<std::string>& levelData,
                    std::vector<Zombie>& zombies, std::vector<Human>& humans,
                    float deltaTime)

Here you can see too many arguments are passed to player class and also need more in the future. But I read many people saying the it is bad to have more than two or three arguments in a function. And In my game there are many classes which take more than 5 or 6 arguments.

Is this right for a game Or Is it is a bad practice? Please tell me how can i solve this problem.

Sorry for bad English. Thank you very much for reading.

Upvotes: 2

Views: 4486

Answers (4)

brucehi
brucehi

Reputation: 26

use a struct contians the similar argumensts, such as:

struct Shape {
  float a_radius;
  float a_rotation;
  float a_depth;
  Color a_color;
  // ...
};

Player::Player(Shape& shape);

Upvotes: 0

Richard Hodges
Richard Hodges

Reputation: 69902

If you're really concerned about it you can pass arguments in a struct. It's no less efficient as far as the compiler is concerned.

If you have c++17, you can unpack the struct with a structured binding for readability.

If you have c++20 you can create the struct with named initialisation.

e.g:

#include <string>

struct vec {
    double x, y, z;
};

struct func_args {
    int a;
    std::string b;
    double c;
    vec x , y , z;
};

void foo(std::string const&, vec const&);

void func(func_args const& args) {
    // c++17 feature - structured binding (in this case by reference)
    auto& [a, b, c, x, y, z] = args;
    foo(b, x);
}

// or by value...
void func(func_args&& args) {
    auto [a, b, c, x, y, z] = std::move(args);
    foo(b, x);
}

void test()
{
    // c++20 feature (and gcc extension prior). Named initialisation.
    func({ .a = 0, .b = "alien", .c = 0.0, 
        .x = { -1, 1, 0}, 
        .y = { -1, 1, 0}, 
        .z = { -1, 1, 0}});

    auto const args2 = func_args { 
        .a = 0, .b = "alien", .c = 0.0, 
        .x = { -1, 1, 0}, 
        .y = { -1, 1, 0}, 
        .z = { -1, 1, 0}};
    func(args2);
}

C++ looks more like javascript every year :)

Upvotes: 0

lalit gangwar
lalit gangwar

Reputation: 401

Having too many arguments in function is bad, but it is not scalable and clean. It cause user of function inconvenient and for you it would be hard to maintain also. You can not add or delete parameter in function it would take lots of time to do that. One more thing you don't need to make too long struct also. If struct become too long split into meaningful structs.

Upvotes: 0

Thomas Caissard
Thomas Caissard

Reputation: 866

Having too many argument is bad only if you call your function a lot of time in your program as you pay the cost of putting the parameter of your function on the stack plus the indirection of the function call.

If you are not calling those functions a lot of time then don't bother packing the variables.

If you need the pack the variable, you can just put them into a struct and pass a single argument to the function (usually a reference to avoid the copy)!

Upvotes: 1

Related Questions