Building a Slackbot 10: Clean Code


So last time I thought I was almost done with the code, but I started reading some posts about clean code and decided to try to rewrite mine to be more readable. This is actually way harder than I thought it would be. It was a lot of re-writing and testing to see if functions still worked and trying to separate bits and pieces to be more modular. I’m just getting started in the idea of clean code so I’m not really sure what I should be trying to do exactly, but my initial goal was to get rid of as much nesting as possible.

Check out my progress in the last few days:

Before


controller.on('slash_command', function (bot, message) {
    // reply to slash command
    oombawDB.checkAddUser(message).then(currentUser => {
      let userObj = currentUser.toObject();
      if (userObj.hasOwnProperty('translateTo')) {
        //bot.whisper(message, "translating...");
        let original = message.text;
        translateWord(currentUser, original).then(res => {
          bot.replyPrivate(message, res.original + ": " + res.translated);
          saveYesOrNo(currentUser, res, message);
        });
      } else {
        let original = message.text;
        bot.replyPrivate(message, {
          text: "What language would you like to translate to?",
          response_type: "ephemeral",
          attachments: [{
            //"text": "Choose a language to translate to",
            fallback: "",
            color: "#3AA3E3",
            attachment_type: "default",
            callback_id: "language_selection",
            actions: [{
              name: "language_choice",
              text: "Pick a language...",
              type: "select",
              options: languages
            }]
          }]
        });

        controller.on('interactive_message_callback', function (bot, message) {
          //bot.whisper(message, 'preferences saved ' + original);
          if (message.callback_id == "language_selection") {
            oombawDB.addUserPref(message, message.text).then(currentUser => {

              translateWord(currentUser, original).then(res => {

                bot.replyInteractive(message, {
                  text: res.original + ": " + res.translated,
                  replace_original: true,
                  callback_id: 'language_selection',
                  response_type: 'ephemeral'
                }, (err) => {
                  if (err) {
                    console.log(err);
                  } else {
                    console.log('Experiment finished')
                  }
                });
                //bot.whisper(message, res);
                saveYesOrNo(currentUser, res, message);

              });
            });
          }

        });
      }
    })

After


controller.on('slash_command', function(bot, message) {
    // reply to slash command

    oombawDB.checkAddUser(message.team_id, message.user_id)
      .then(oombawUser => helper.checkLanguagePrefs(oombawUser)
        ? hasLangPrefsPath(oombawUser, message)
        : setupPrefsPath(oombawUser, message))

  });

  function setupPrefsPath(oombawUser, message) {
    // TODO reply to message and begin convo chain
    console.log("wip");
  }

  function hasLangPrefsPath(oombawUser, message) {
    translateWord(oombawUser, message.text)
      .then(oombawUser => {
        bot.replyPrivate(message, oombawUser.temp.original + ": " + oombawUser.temp.translated);
        saveYesOrNo(oombawUser, message);
      })
      .catch(console.error)
  }

Obviously there are still things I have to do, hence the WIP, but I do feel like my code is much more readable now.