Visual Studio Code

Visual Studio Code Challenge 2: Refactoring

Check out a free preview of the full Visual Studio Code course:
The "Challenge 2: Refactoring" Lesson is part of the full, Visual Studio Code course featured in this preview video. Here's what you'd learn in this lesson:

In this challenge, students refactor code through two exercises.

Get Unlimited Access Now

Transcript from the "Challenge 2: Refactoring" Lesson

>> Mike North: So here is our exercise. We've got this listener support module. If you click on this, it should take you right to the file and it does. So we wanna change register and unregister to registerListener, unregisterListener. So that's gonna be your first refactor. The second one is, and let me explain the purpose of this object.

[00:00:21] It basically just lets you register functions, and we accumulate those functions in an array. And then we can call this function, we can call it fire on a listener support instance. And it'll basically go through that array and invoke all of those functions one by one. It's just it is like a low-level thing that is used for allowing concerned parties to subscribe in the event that something happens.

[00:00:50] And then fire is what we're gonna use when we want to say, okay, broadcast to all concerned parties that this is happening. So that's all that this does. So we basically wanna just rename these register and unregister, so that they are a little bit more descriptive. But we wanna also change the format of how fire works.

[00:01:08] So we wanna pass it, currently it's just taking in anything at all, and it's passing that directly to the functions. We wanna now wrap it in an object that looks like this, so that the thing that should be passed to those functions is available at the data property of whatever is passed as an argument to fire.

[00:01:29] So we pass something like this to fire, but the value of that data property that is what should be passed into each of those listener functions as they're invoked. So for this you're going to need to, you're gonna need to go through and figure out who's using this, make sure that they all remain untouched.

[00:01:55] Who's using the fire function rather, so they're not gonna remain untouched. They'll have to each change, so you'll probably wanna use find all references. Oops, sorry, this here is a typo. I'm actually gonna fix this right now. This should not be in this exercise. We'll remove that. So you wanna ensure that wherever someone's calling fire, they need to be updated to accommodate this new type of structure.

[00:02:21] And anything that is registering a listener or updating a listener, the only thing that should change in those files is the names of the functions. Register, unregister should be registerListener or unregisterListener. The callback should just be able to proceed as is. Nothing should change there. So this should give you some exposure to a limited subset of Visual Studio Code's refactoring capabilities.