Integrated Development Environments Superpowers 13 exercises

Refactoring with Quick Fixes

It's important to note what you target with your Quick Fix, as it determines the available options.

Inlining Variables

If you target randomPercentage, you can select an "Inline variable" option.

This would remove the variable and inline its value into the console.log:


Loading solution


00:00 So let's give this a go. Now, it's really important what you target with your Quick Fix. If you target random percentage here, you're gonna get some different options. You've got inline variable here. Let's try this out. Well, what that does is instead of using just logging the random percentage here, it actually just removes the name completely

00:17 and takes this and just inlines it into the console log. Let's try that again. Inline variable, bam. Very nice. Notice how fast it did that as well. I just love this stuff. Okay, cool. What if we do, let's say, just grab this little bit here. And again, using command dot,

00:35 we can say fix using copilot, explain using copilot. Extract a constant in closing scope. Let's try that. Ooh, we got new local. Okay, so random times 100. Let's try that. Beautiful. And we can save and then pretty, it will sort of clean everything up. That's really nice. Let's try the opposite of this.

00:54 Inline it again. I just love playing with this stuff. How about we actually get back to what we're supposed to be doing and taking this whole section, this whole piece of code, and just saying extract to function in module scope. If we extract it to constant in module scope, then it will do this,

01:13 which is not necessarily what we want. We want it to do extract to function. And now we get to name our new function. We can say get random percentage like this. And now it all just works beautifully. So now we've got a function. We can reuse in different places for get random percentage here. We can even move it to a new file if we want to.

01:33 These built-in just quick fixes are so useful. And you can really do a lot with them that I probably can't even cover in the scope of this chapter.