Skip to content

Instantly share code, notes, and snippets.

@laurenfazah

laurenfazah/refactor.md

Last active May 31, 2019
Embed
What would you like to do?
if (weatherCode === "good"){
    body.removeClass(currentClass);
    body.addClass("good");
    //dialog.css("background-color", "rgb(169,247,188)");
    return "Looks like a great day to ride!";
  } else if (weatherCode === "maybe"){
    body.removeClass(currentClass);
    body.addClass("maybe");
    //dialog.css("background-color", "rgb(243,212,122)");
    return "You could ride, but be Vcareful out there!";
  } else if (weatherCode === "bad"){
    body.removeClass(currentClass);
    body.addClass("bad");
    //dialog.css("background-color", "rgb(243,152,122)");
    return "It's probably not the best day to ride.";
  } 
}
  1. remove repeated code within logic branches:
body.removeClass(currentClass);
if (weatherCode === "good"){
    body.addClass("good");
    //dialog.css("background-color", "rgb(169,247,188)");
    return "Looks like a great day to ride!";
  } else if (weatherCode === "maybe"){
    body.addClass("maybe");
    //dialog.css("background-color", "rgb(243,212,122)");
    return "You could ride, but be Vcareful out there!";
  } else if (weatherCode === "bad"){
    body.addClass("bad");
    //dialog.css("background-color", "rgb(243,152,122)");
    return "It's probably not the best day to ride.";
  } 
}
  1. leverage weatherCode variable's value to remove more repeated code:
body.removeClass(currentClass);
body.addClass(weatherCode);
if (weatherCode === "good"){
    //dialog.css("background-color", "rgb(169,247,188)");
    return "Looks like a great day to ride!";
  } else if (weatherCode === "maybe"){
    //dialog.css("background-color", "rgb(243,212,122)");
    return "You could ride, but be Vcareful out there!";
  } else if (weatherCode === "bad"){
    //dialog.css("background-color", "rgb(243,152,122)");
    return "It's probably not the best day to ride.";
  } 
}
  1. remaining logic branches can be collapsed as well! leverage weatherCode variable's value to remove more repeated code:
var weatherData = {
    "good": {
        color: "rgb(169,247,188)",
        text: "Looks like a great day to ride!"
      },
    "maybe": {
        color: "rgb(243,212,122)",
        text: "You could ride, but be Vcareful out there!"
     },
    "bad": {
        color: "rgb(243,152,122)",
        text: "It's probably not the best day to ride."
      }
}

body.removeClass(currentClass);
body.addClass(weatherCode);
dialog.css("background-color", weatherData[weatherCode]["color"]);
return weatherData[weatherCode]["text"];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment