Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Simplifying chained conditional block #7

Open
Huy-Ngo opened this issue Oct 5, 2020 · 0 comments
Open

[Refactor] Simplifying chained conditional block #7

Huy-Ngo opened this issue Oct 5, 2020 · 0 comments

Comments

@Huy-Ngo
Copy link
Collaborator

Huy-Ngo commented Oct 5, 2020

Currently, the command are called with chained if-else blocks like this:

if (message === 'command 1') {
    http.get({
        hostname: config.apiHost,
        port: config.apiPort,
        path: 'api/path/1'
    }, sendMeme);
} else if (message === 'command 2') {
    http.get({
        hostname: config.apiHost,
        port: config.apiPort,
        path: 'api/path/2'
    }, sendMeme);
} else ...

This is terrible, because:

  • large amount of repetition
  • high time complexity O(n)

Ideally, after this refactoring, the above code would look like this:

commandMap = {
  'command 1': {
    'path': 'api/path/1',
    'help': 'This command do thing 1'
  },
  'command 2': {
    'path': 'api/path/2',
    'help': 'This command do thing 2'
};
addMemeCommand(commandMap);

This will makes it easier to add a command. Moreover, it has low time complexity O(1) and it is also easier to implement a solution for #6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant