VS Code Can Do That - Workshop
1.0.0
1.0.0
  • Introduction
  • Essential Keyboard Shortcuts
  • Exercise 1 - Customizing The Editor
    • Before you start
    • Switch themes
    • Install a new icon theme
    • Switch fonts
    • Editor tweaks
    • Change default Settings view
    • Easily identify editor instances
  • Exercise 2 - Productivity Tricks
    • Before you start
    • Essential navigation shortcuts
    • Creating HTML with Emmet
    • Styling with Emmet
    • Update image sizes
  • Exercise 3 - Navigation And Refactoring
    • Before you start
    • Moving, Duplicating and Deleting
    • Folding sections
    • Multiple cursors
    • Rename refactor
    • Finding things
    • Extract refactor
  • Exercise 4 - Debugging
    • Before you start
    • Simple debugging
    • Simple launch config
    • Auto attach
    • Debugging browser apps
    • Compound debug configurations
  • Exercise 5 - Docker
    • Untitled
Powered by GitBook
On this page

Was this helpful?

  1. Exercise 3 - Navigation And Refactoring

Extract refactor

PreviousFinding thingsNextBefore you start

Last updated 5 years ago

Was this helpful?

It's common to realize that a certain block of code is going to be used in multiple places. When you realize that, you usually want to extract this code into it's own method. VS Code includes an "Extract Method" refactoring tool to make this easier.

Extract the code in the connection.on method inside the constructor into a method called updateColor

  • Open the "app.js" file

  • Highlight the code inside of the connection.on AFTER the "// update the bulb color" comment. Do not include the comment line.

// receives the "colorChanged" web socket event
connection.on("colorChanged", hex => {
  // update the bulb color
  bulb.style = `fill: #${hex};`;
  currentColor.textContent = `#${hex}`
});
  • Press Cmd/Ctrl + .

  • Select "Extract method in class "Application"

  • Name the method "updateColor"

  • Press Enter

  • Notice that the code has been extracted to a new method in the Application class called updateColor. Also notice that the variable hex was automatically included as a parameter.