coding-challenges
coding-challenges copied to clipboard
Make the code for challenge 8 more modular and scalable
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.