Creating a C++ Class Instance for every server request?

46 views Asked by At

Me and my team are writing an IRC server in C++ for a school project and we are hung up on the question of performance vs readability when it comes to working with client requests. Basicaly we receive an input, where the first word will determine, which command is then called on the server, and in IRC, different commands have different syntaxes.

Idea 1: We have a Class called Command, and subclasses for each type of command. As they have different syntax, you have to "tokenize" every command differently. Everytime a request arrives, an instance of Class Command will determine the Subcommand, whose class instance will be created to finish "tokenization".

Idea 2: We have a Class called Command, one instance will be created throughout the entire time the server is active. Whenever we receive a request with a command we just reassign the variables within the instance.

Idea 3: We just go monolithic for processing the commands. We determine the command to be called based on the first word, then in a function we just split the string, and implement the command.

One part of the team says, that Idea 1 is too heavy and produces too much load per request that renders the server inefficient. the other says, that although it does have a slower performance, the other ideas are messy and the Class structure of Idea 1 introduces more readability and easier maintainability.

1

There are 1 answers

0
Casey On

The advantage of approach 1 over approach 3 is readability and less mental anguish.

If you were to ask "Hey, where is the code for Broadcast Message handled?" The answer for approach 1 would be: "It's in the BroadcastMessageCommand class."; approach 3 is: "It's somewhere in this massive single function called HandleCommands. Good luck."

Readability over performance until proven otherwise WITH PROFILING AND MEASURING is my go-to. That all said, this question is way too opinion-based and may be closed outright anyway.