coding-challenges icon indicating copy to clipboard operation
coding-challenges copied to clipboard

Make the code for challenge 8 more modular and scalable

Open jainmohit2001 opened this issue 1 year ago • 1 comments

Currently the commands in the server implementation are handled via switch case statements as follows:

switch (command) {
  case RedisCommands.PING:
    this.handlePing(sock, data);
    break;
  case RedisCommands.ECHO:
    this.handleEcho(sock, data);
    break;
  case RedisCommands.SET:
    this.handleSet(sock, data);
    break;
  case RedisCommands.GET:
    this.handleGet(sock, data);
    break;
  case RedisCommands.DEL:
    this.handleDelete(sock, data);
    break;
  default:
    throw new Error(`UNKNOWN_COMMAND: ${command}`);
}

This way of writing code is not scalable.

What we can do is create a new folder, say commands, add the respective commands, and export them using module.exports.

We can take the sample code from Challenge 19 as a reference and build the command handlers in a similar manner.

jainmohit2001 avatar Aug 30 '23 06:08 jainmohit2001