Skip to content

Proposal: Introduce Options structs (parameter objects) for API methods. #362

Description

@TiptevDmitry

Hello everyone!

Problem:

Methods in Api.h currently have very long parameter lists. For example, sendMessage takes 11 parameters. This leads to cumbersome function calls with many default arguments (e.g., nullptr, 0, or false) just to access a specific parameter at the end of the signature.

For example, if I only need to set the businessConnectionId, I'm forced to write the following:

bot.getApi().sendMessage(
id,
textMessage,
nullptr,
nullptr,
nullptr,
"",
false,
std::vector<TgBot::MessageEntity::Ptr>(),
0,
false,
businessConnectionId
);

Writing 8 lines of default values ​​takes up space and makes the code difficult to read, as it's impossible to understand what each nullptr or false value means at a glance.

Proposed solution:

Introduce parameter structures (Options structures) for methods with multiple optional parameters (e.g., SendMessageOptions, PromoteChatMemberOptions, etc.).

The call would then look like this:

SendMessageOptions sendMessageOptions{};

sendMessageOptions.businessConnectionId = businessConnectionId;

bot.getApi().sendMessage(id, textMessage, sendMessageOptions); 

This will significantly improve readability. We know exactly which optional parameters are changing, and we write less code.

Questions for the community and @reo7sp:

Since updating the Api class is a major architectural change, I'd like to consult with you before starting:

  1. Is this change desirable?

  2. Should existing methods be refactored or overloaded (to ensure backward compatibility with previous versions)?

  3. Should structs contain only optional parameters (keeping mandatory ones, such as chatId and text, in the function signature) or all parameters?

I'd appreciate your thoughts and feedback on this!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions